zoukankan      html  css  js  c++  java
  • git简单使用

    公司内部代码管理使用git,简单使用一下,还蛮好的。

    我简单的理解是:在本地建立文件夹,可以放project,里面什么file、dir都可以放,放完了之后,提交到git里,可以供展示,或者是别人下载更新之类。高级的用法目前没用到。

    1.全局设置

    Git global setup
    
    git config --global user.name "xuecheng.hu"
    git config --global user.email "hxc@aispeech.com"

    2.建仓提交

    Create a new repository
    
    git clone https://gitlab.spetechcular.com/xuecheng.hu/test2.git //当前git上的地址
    cd test2    //已经克隆到本地目录了,直接进去
    touch README.md  //各种花里胡哨操作,可以把自己的代码移进来,只要不提交,这就相当于本地文件夹
    git add README.md     //提交
    git commit -m "add README"  // 提交时的评论,方便看,这提交的是个啥东西
    git push -u origin master   // 推送到master的这个分支,一般情况下都是这个,可以换别的

    以上我的事就完成了,下面的我都没怎么看,用到再说~~

    常用操作

    这里只是流水账似的列出常用的几个操作, 要想真正用得熟练, 还得多看文档掌握重要的git命令如branch, checkout, push, pull, merge, fetch, reset等

    创建新分支

    $ git branch <branch-name>
    

    签出分支

    也叫切换分支

    $ git checkout <branch-name>
    

    列出所有branch

    标星号的是当前branch

    $ git branch -a 
    

    删除本地分支

    $ git branch -d <local-branch-name>
    

    删除远程分支

    $ git push origin --delete <remote-branch-name>
    

    clone一个分支

    $ # 为了规避502错误或跟踪其它分支, 若出现502再使用--depth 1
    $ git clone https://gitlab.example.com/shun.zhang/aispeech-git-help.git -b <branch-name> [--depth 1]
    
    $ # 再增加跟踪一个分支
    $ git pull origin <branch-name>:<branch-name>
    $ echo <branch-lastest-commit> > .git/refs/remotes/origin/<branch-name>
    

    查看本地修改

    $ git diff
    $ git difftool  # diff和difftool都可以
    

    提交本地修改

    $ git commit -m "commit log" <file-pattern> # 提交部分
    $ git commit -m "commit log" -a             # 提交所有
    

    推送到远程仓库

    $ git push origin <branch-name>
    

    回滚

    恢复到某个文件到最近一次提交的版本/取消某个文件的本地修改

    $ git checkout <file-name>
    

    恢复某个文件到历史版本

    $ git log <file-name>                   # 查看日志中的commit-number
    $ git reset <commit-number> <file-name>
    $ git checkout <file-name>
    

    撤消本地commit(本地commit了, 但未push到服务端)

    git reset --hard <commit-number>
    

    同步当前分支的更新

    例如当前feature分支还在开发中, 其它小伙伴向当前提交了代码 不建议使用pull命令, 因为它隐藏了一些细节, 这里用fetch & merge命令替代, 每一步心里有数

    $ git fetch origin <branch-name>              # 下载最新的代码到远程跟踪分支, 即origin/<branch-name>
    $ git difftool <branch-name> origin/<branch-name> # 查看更新内容, 看看而已, 不要在这一步执行手工merge
    $ git merge origin/<branch-name>              # 尝试合并远程跟踪分支的代码到本地分支
    $ git mergetool                               # 借助mergetool解决冲突
    

    同步其它分支的更新

    例如当前feature分支还在开发中, master上已经有了更新的代码, 和上例同步当前分支更新的步骤类似

    $ git fetch origin master
    $ git difftool <branch-name> origin/master
    $ git merge origin/master
    $ git mergetool
    
    $ # git merge --abort # 回到merge前的状态
    

    fork后同步上游仓库的更新

    如下示例里假设你fork了shun.zhang/aispeech-git-help, 你想享受到上游的更新

    $ git remote -v # 查看远程仓库
    origin https://gitlab.example.com/li.li/aispeech-git-help.git (push)
    origin https://gitlab.example.com/li.li/aispeech-git-help.git (fetch)
    
    $ git remote add upstream https://gitlab.example.com/shun.zhang/aispeech-git-help.git # 第一次需要添加上游仓库
    $ git remote -v
    origin  https://gitlab.example.com/li.li/aispeech-git-help.git (push)
    origin  https://gitlab.example.com/li.li/aispeech-git-help.git (fetch)
    upstream    https://gitlab.example.com/shun.zhang/aispeech-git-help.git (push)
    upstream    https://gitlab.example.com/shun.zhang/aispeech-git-help.git (fetch)
    
    
    $ git fetch upstream # 和上例同步当前分支更新的步骤类似了
    $ git difftool <branch-name> upstream/master
    $ git merge upstream/master
    $ git mergetool
    

    引用公共代码

    代码引用在git上有两种方式: submodule和subtree, 推荐使用subtree方式, submodule需要每个人都有公共项目的权限, 而subtree只需要一个有权限访问公共库的人来维护就行了, 对团队其它成员是透明的 虽然可以直接引用代码库, 但是基础项目尽量对外发布二进制库, 而不是开放源代码给其它项目直接引用

    $ # 第一次初始化
    $ git remote add -f <remote-subtree-repository> <remote-subtree-repository-url>
    $ git subtree add --prefix=<local-directory> <remote-subtree-repository> <remote-subtree-branch-name> --squash
    
    $ # 同步subtree的更新
    $ git fetch <remote-subtree-repository> <remote-subtree-branch-name>
    $ git subtree pull --prefix=<local-directory> <remote-subtree-repository> <remote-subtree-branch-name>
    

    project管理

    create

    在网页上创建, 默认限制一个用户最多创建3个项目, 如需创建更多项目请与管理员联系

    fork

    只有当你有某个项目的读权限但是没有写权限时才需要fork, 这在开源界用的多, 但在公司内部基本上都是private项目, 所以使用不多

    permission

    所有项目默认是private, 即只对该项目组内的成员开放权限, 项目的owner(默认是创建者)负责项目的访问权限设置, 并且可以对每一个成员设置权限

    group

    有时一个项目比较大, 会有很多子项目, 这时可以专门为这个大项目创建一个分组, 例如aios分组下有aios-coreaios-for-car, 语音内核core分组下有asr-cloudasr-local

    如需创建group请与管理员联系

    transfer

    可以将一个项目过户给另一个用户, 比如将 shun.zhang 创建的 aispeech-git-help 过户给 li.li, 过户后的地址就变成了 https://gitlab.example.com/li.li/aispeech-git-help

    常见问题

    • 用什么编辑markdown文本 -- dillinger 是优秀的markdown在线编辑器
    • gitlab clone时报502错误 -- 加参数--depth 1
    • Your branch and 'origin/master' have diverged -- 执行git pull --rebase
    • 支持TOC -- README.MD第一行使用标签[[_TOC_]]gitlab支持TOC
  • 相关阅读:
    logcat 自动清屏
    eclipse debug (调试) 学习心得
    黑马面试题
    如何分析解决Android ANR
    植物大战僵尸(一)
    cocos2d-小游戏
    VIM编辑器的使用
    面试题之排序总结
    面试题链表总结
    微软大楼设计方案(中等)(2017 计蒜之道 初赛 第六场)
  • 原文地址:https://www.cnblogs.com/dahu-daqing/p/6737892.html
Copyright © 2011-2022 走看看