zoukankan      html  css  js  c++  java
  • Git的多人协作模式

    多人协作的工作模式通常是这样:
    1. 首先,可以试图用git push origin <branch-name>推送自己的修改;
    2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
    3. 如果合并有冲突,则解决冲突,并在本地提交;
    4. 没有冲突或者解决掉冲突后,再用git push origin <branch-name>推送就能成功!
    5. 如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>
    6. 这就是多人协作的工作模式,一旦熟悉了,就非常简单。
    我们模仿下多人在操作同一个远程分支造成的冲突问题:
    开发A和开发B 
    我们先让开发A创建分支:
    [root@VM-75-64 test]# git checkout -b dev
    Switched to a new branch 'dev'
    [root@VM-75-64 test]# git branch
    * dev
      master
    [root@VM-75-64 test]# git push origin dev
    Total 0 (delta 0), reused 0 (delta 0)
    remote:
    remote: To create a merge request for dev, visit:
    remote:
    To git@192.168.75.64:root/origin-test.git
    * [new branch]      dev -> dev
    [root@VM-75-64 test]# git branch -r
      origin/dev
      origin/master
    [root@VM-75-64 test]# git branch
    * dev
    master
     
    开发B:
    [root@VM-75-64 origin-test]# git pull                #这里一定要pull,不然看不到远程dev分支
    From 192.168.75.64:root/origin-test
    * [new branch]      dev        -> origin/dev
    Already up-to-date.
    [root@VM-75-64 origin-test]# git branch -r
      origin/HEAD -> origin/master
      origin/dev
      origin/master
     
    此时开发B修改test文件
    [root@VM-75-64 origin-test]# git checkout -b dev origin/dev    #创建跟远程dev分支相同的本地dev分支,名字尽量相同
    Branch dev set up to track remote branch dev from origin.
    Switched to a new branch 'dev'
    [root@VM-75-64 origin-test]# vim test.txt
    lalala
    ni hao ya
    another test
    deploy test by B                                                #添加修改内容
    [root@VM-75-64 origin-test]# git add test.txt
    [root@VM-75-64 origin-test]# git commit -m 'deploy test by B'
    [dev b5f166b] deploy test by B
    1 files changed, 1 insertions(+), 0 deletions(-)
    [root@VM-75-64 origin-test]# git push origin dev                #推动到远程dev
    Counting objects: 5, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 318 bytes, done.
    Total 3 (delta 0), reused 0 (delta 0)
    remote:
    remote: To create a merge request for dev, visit:
    remote:
    To git@192.168.75.64:root/origin-test.git
       68b1e43..b5f166b  dev -> dev
     
    此时开发A也开始修改test文件:
    [root@VM-75-64 test]# vim test.txt
    lalala
    ni hao ya
    another test
    deploy test by A
    [root@VM-75-64 test]# git add test.txt
    [root@VM-75-64 test]# git commit -m 'deploy by A'
    [dev 152475d] deploy by A
    1 files changed, 1 insertions(+), 0 deletions(-)
    [root@VM-75-64 test]#
    [root@VM-75-64 test]# git push origin dev                    #在推送到远程的时候报错了!
    To git@192.168.75.64:root/origin-test.git
    ! [rejected]        dev -> dev (non-fast-forward)
    error: failed to push some refs to 'git@192.168.75.64:root/origin-test.git'
    To prevent you from losing history, non-fast-forward updates were rejected
    Merge the remote changes before pushing again.  See the 'Note about
    fast-forwards' section of 'git push --help' for details.
    ####这里跟网上的报错实例不同,并没有提示该怎么操作~
    ####原因是此时远程dev分支高于你本次的修改,存在冲突
    修复方法:
    1:pull 最新远程分支到本地
    [root@VM-75-64 test]# git pull origin dev
    remote: Enumerating objects: 5, done.
    remote: Counting objects: 100% (5/5), done.
    remote: Compressing objects: 100% (2/2), done.
    remote: Total 3 (delta 0), reused 3 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From 192.168.75.64:root/origin-test
    * branch            dev        -> FETCH_HEAD
    Auto-merging test.txt                            #####here
    CONFLICT (content): Merge conflict in test.txt
    Automatic merge failed; fix conflicts and then commit the result.
     
    此时会提示你具体的冲突文件!
    手动解决冲突~编辑test.txt文件:
    [root@VM-75-64 test]# cat test.txt
    lalala
    ni hao ya
    another test
    <<<<<<< HEAD
    deploy test by A
    =======
    deploy test by B
    >>>>>>> b5f166b16b314b9d2f102b990a19bb40c7a4728c        #提示你冲突的位置
    [root@VM-75-64 test]# vim test.txt
    [root@VM-75-64 test]# cat test.txt
    lalala
    ni hao ya
    another test
    deploy test by A and B                                #这里是编辑的最终版
    [root@VM-75-64 test]# git add test.txt
    [root@VM-75-64 test]# git commit -m 'final test'        #上传并提交
    [dev 184bd71] final test
    [root@VM-75-64 test]# git push origin dev               #推送远程dev分支!成功!
    Counting objects: 10, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (5/5), done.
    Writing objects: 100% (6/6), 627 bytes, done.
    Total 6 (delta 0), reused 0 (delta 0)
    remote:
    remote: To create a merge request for dev, visit:
    remote:
    To git@192.168.75.64:root/origin-test.git
       b5f166b..184bd71  dev -> dev
     
     
    以上就是常见的单个分支冲突的解决方法!
    小结:
    • 查看远程库信息,使用git remote -v
    • 本地新建的分支如果不推送到远程,对其他人就是不可见的;
    • 从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
    • 在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
    • 建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name;(git branch --set-upstream-to=origin/dev dev,这两条指令都可以
    • 从远程抓取分支,使用git pull,如果有冲突,要先处理冲突
     
     以上,共勉!
  • 相关阅读:
    安全系列之二:OAuth2.0 开放授权协议
    安全系列之一:忘记密码
    Tomcat剖析(五):Tomcat 容器
    如何做好项目?
    SonarLint(Sonar) 代码质量管理
    一个JavaWeb项目开发总结
    Tomcat剖析(四):Tomcat默认连接器(2)
    Tomcat剖析(四):Tomcat默认连接器(1)
    Spring mvc 字节流
    spring mvc 之初体验
  • 原文地址:https://www.cnblogs.com/storyawine/p/13408347.html
Copyright © 2011-2022 走看看