• 推送分支
  • 抓取分支
  • 小结
    1. $ git remote
    2. origin

    或者,用git remote -v显示更详细的信息:

    1. $ git remote -v
    2. origin git@github.com:michaelliao/learngit.git (fetch)
    3. origin git@github.com:michaelliao/learngit.git (push)

    上面显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。

    推送分支

    推送分支,就是把该分支上的所有本地提交推送到远程库。推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上:

    1. $ git push origin master

    如果要推送其他分支,比如dev,就改成:

    1. $ git push origin dev

    但是,并不是一定要把本地分支往远程推送,那么,哪些分支需要推送,哪些不需要呢?

    • master分支是主分支,因此要时刻与远程同步;

    • dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;

    • bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;

    • feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。

    总之,就是在Git中,分支完全可以在本地自己藏着玩,是否推送,视你的心情而定!

    抓取分支

    多人协作时,大家都会往masterdev分支上推送各自的修改。

    现在,模拟一个你的小伙伴,可以在另一台电脑(注意要把SSH Key添加到GitHub)或者同一台电脑的另一个目录下克隆:

    1. $ git clone git@github.com:michaelliao/learngit.git
    2. Cloning into 'learngit'...
    3. remote: Counting objects: 46, done.
    4. remote: Compressing objects: 100% (26/26), done.
    5. remote: Total 46 (delta 16), reused 45 (delta 15)
    6. Receiving objects: 100% (46/46), 15.69 KiB | 6 KiB/s, done.
    7. Resolving deltas: 100% (16/16), done.

    当你的小伙伴从远程库clone时,默认情况下,你的小伙伴只能看到本地的master分支。不信可以用git branch命令看看:

    1. $ git branch
    2. * master

    现在,你的小伙伴要在dev分支上开发,就必须创建远程origindev分支到本地,于是他用这个命令创建本地dev分支:

    1. $ git checkout -b dev origin/dev

    现在,他就可以在dev上继续修改,然后,时不时地把dev分支push到远程:

    1. $ git commit -m "add /usr/bin/env"
    2. [dev 291bea8] add /usr/bin/env
    3. 1 file changed, 1 insertion(+)
    4. $ git push origin dev
    5. Counting objects: 5, done.
    6. Delta compression using up to 4 threads.
    7. Compressing objects: 100% (2/2), done.
    8. Writing objects: 100% (3/3), 349 bytes, done.
    9. Total 3 (delta 0), reused 0 (delta 0)
    10. To git@github.com:michaelliao/learngit.git
    11. fc38031..291bea8 dev -> dev
    你的小伙伴已经向origin/dev分支推送了他的提交,而碰巧你也对同样的文件作了修改,并试图推送:
    1. $ git add hello.py
    2. $ git commit -m "add coding: utf-8"
    3. [dev bd6ae48] add coding: utf-8
    4. 1 file changed, 1 insertion(+)
    5. $ git push origin dev
    6. To git@github.com:michaelliao/learngit.git
    7. ! [rejected] dev -> dev (non-fast-forward)
    8. error: failed to push some refs to 'git@github.com:michaelliao/learngit.git'
    9. hint: Updates were rejected because the tip of your current branch is behind
    10. hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
    11. hint: before pushing again.
    12. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突,解决办法也很简单,Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,然后,在本地合并,解决冲突,再推送:

    1. $ git pull
    2. remote: Counting objects: 5, done.
    3. remote: Compressing objects: 100% (2/2), done.
    4. remote: Total 3 (delta 0), reused 3 (delta 0)
    5. Unpacking objects: 100% (3/3), done.
    6. From github.com:michaelliao/learngit
    7. fc38031..291bea8 dev -> origin/dev
    8. There is no tracking information for the current branch.
    9. Please specify which branch you want to merge with.
    10. See git-pull(1) for details
    11. git pull <remote> <branch>
    12. If you wish to set tracking information for this branch you can do so with:
    13. git branch --set-upstream dev origin/<branch>

    git pull也失败了,原因是没有指定本地dev分支与远程origin/dev分支的链接,根据提示,设置devorigin/dev的链接:

    1. $ git branch --set-upstream dev origin/dev
    2. Branch dev set up to track remote branch dev from origin.

    再pull:

    1. $ git pull
    2. Auto-merging hello.py
    3. CONFLICT (content): Merge conflict in hello.py
    4. Automatic merge failed; fix conflicts and then commit the result.

    这回git pull成功,但是合并有冲突,需要手动解决,解决的方法和分支管理中的解决冲突完全一样。解决后,提交,再push:

    1. $ git commit -m "merge & fix hello.py"
      [dev adca45d] merge & fix hello.py
      $ git push origin dev
      Counting objects: 10, done.
      Delta compression using up to 4 threads.
      Compressing objects: 100% (5/5), done.
      Writing objects: 100% (6/6), 747 bytes, done.
      Total 6 (delta 0), reused 0 (delta 0)
      To git@github.com:michaelliao/learngit.git
      291bea8..adca45d dev -> dev

    因此,多人协作的工作模式通常是这样:

    • 首先,可以试图用gitpushoriginbranch-name推送自己的修改;

    • 如果推送失败,则因为远程分支比你的本地更新,需要先用gitpull试图合并;

    • 如果合并有冲突,则解决冲突,并在本地提交;

    • 没有冲突或者解决掉冲突后,再用gitpushoriginbranch-name推送就能成功!

    如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch —set-upstream branch-name origin/branch-name

    这就是多人协作的工作模式,一旦熟悉了,就非常简单。

    小结

    原文:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013760174128707b935b0be6fc4fc6ace66c4f15618f8d000