Git分支-团队协作

在上节中,已经了解了基本的分支管理,在实际的项目中,肯定是存在远程仓库和团队协作的。下面就来重点讲解,在团队多人协作中的分支推送与抓取。

准备工作

  1. 在GitHub上创建新仓库”worktogether”,并添加文件”README.md”。
  2. Clone远程库到本地。 实际上Git自动把本地的master分支和远程的master分支对应起来了,且远程仓库的默认名称是origin。

查看远程

  • 查看

    1
    2
    $ git remote
    origin
  • 查看详细

    1
    2
    3
    $ git remote -v
    origin https://github.com/yongfeiuall/worktogether.git (fetch)
    origin https://github.com/yongfeiuall/worktogether.git (push)

分支推送

我们在远程仓库那章节已经学习了怎么使用。 详细请参考:远程仓库
修改”README.md”,并提交,推送。

1
2
3
4
5
6
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 286 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/yongfeiuall/worktogether.git
952b8b0..242264d master -> master

Push Result

分支抓取

团队协作时,在PUSH时难免会有冲突的出现,我们主要是看一下这个处理流程。

另一人推送提交

  1. 在另一台电脑clone仓库”worktogether。
  2. 修改文件,并提交。
  3. Push到远程
    1
    2
    3
    4
    5
    6
    7
    8
    $ git push origin master
    Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 307 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To https://github.com/yongfeiuall/worktogether.git
    242264d..5b7966c master -> master

你操作同一个文件

  1. 修改文件,并提交
  2. Push到远程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ git push origin master
    To https://github.com/yongfeiuall/worktogether.git
    ! [rejected] master -> master (fetch first)
    error: failed to push some refs to 'https://github.com/yongfeiuall/worktogether.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    推送失败,因为另一个人的最新提交和你试图推送的提交有冲突。

  3. get最新的提交

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ git pull
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), done.
    From https://github.com/yongfeiuall/worktogether
    242264d..5b7966c master -> origin/master
    Auto-merging README.md
    CONFLICT (content): Merge conflict in README.md
    Automatic merge failed; fix conflicts and then commit the result.
  4. 手动解决冲突,并提交

  5. 再次Push
    1
    2
    3
    4
    5
    6
    7
    8
    $ git push origin master
    Counting objects: 6, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (6/6), 564 bytes | 0 bytes/s, done.
    Total 6 (delta 1), reused 0 (delta 0)
    To https://github.com/yongfeiuall/worktogether.git
    5b7966c..1d2aaa5 master -> master

引用LIAOXUEFENG BLOG

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

  1. 首先,可以试图用git push origin branch-name推送自己的修改;
  2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
  3. 如果合并有冲突,则解决冲突,并在本地提交;
  4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
    如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!