• Git复习(五)之多人协作、git push失败、git pull失败


    多人协作

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

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

    $ git clone git@github.com:michaelliao/learngit.git
    Cloning into 'learngit'...
    remote: Counting objects: 40, done.
    remote: Compressing objects: 100% (21/21), done.
    remote: Total 40 (delta 14), reused 40 (delta 14), pack-reused 0
    Receiving objects: 100% (40/40), done.
    Resolving deltas: 100% (14/14), done.

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

    $ git branch
    * master

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

    $ git checkout -b dev origin/dev

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

    $ git add env.txt
    
    $ git commit -m "add env"
    [dev 7a5e5dd] add env
     1 file changed, 1 insertion(+)
     create mode 100644 env.txt
    
    $ git push origin dev
    Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To github.com:michaelliao/learngit.git
       f52c633..7a5e5dd  dev -> dev

    推送失败

    你的小伙伴已经向origin/dev分支推送了他的提交,而碰巧你也对同样的文件作了修改,并试图推送:

    $ cat env.txt
    env
    
    $ git add env.txt
    
    $ git commit -m "add new env"
    [dev 7bd91f1] add new env
     1 file changed, 1 insertion(+)
     create mode 100644 env.txt
    
    $ git push origin dev
    To github.com:michaelliao/learngit.git
     ! [rejected]        dev -> dev (non-fast-forward)
    error: failed to push some refs to 'git@github.com:michaelliao/learngit.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    推送失败,则因为远程分支比你的本地更新,解决办法也很简单,Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,再推送:

    $ git pull
    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details.
    
        git pull <remote> <branch>
    
    If you wish to set tracking information for this branch you can do so with:
    
        git branch --set-upstream-to=origin/<branch> dev

    拉取失败

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

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

    再pull:

    $ git pull
    Auto-merging env.txt
    CONFLICT (add/add): Merge conflict in env.txt
    Automatic merge failed; fix conflicts and then commit the result.

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

    $ git commit -m "fix env conflict"
    [dev 57c53ab] fix env conflict
    
    $ git push origin dev
    Counting objects: 6, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (6/6), 621 bytes | 621.00 KiB/s, done.
    Total 6 (delta 0), reused 0 (delta 0)
    To github.com:michaelliao/learngit.git
       7a5e5dd..57c53ab  dev -> dev

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

    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-to <branch-name> origin/<branch-name>

     

  • 相关阅读:
    RSS文章订阅及生成RSS格式的xml
    一次Insert插入多条数据的方法
    构建struts2项目需要使用的最少架包
    Java 的HTML 解析器jsoup
    tomcat定时启动
    栈的实现与应用
    软件加密行业的现状和发展趋势
    InstallShield 2012 Spring新功能试用(18): Suite/Advanced UI 和 Advanced UI工程的界面编辑视图中添加了控件工具栏和语言切换功能
    BUG:InstallShield 2012 Spring之前版本的帮助文档中关于StrGetTokens示例代码的错误
    InstallShield 2012 Spring新功能试用(14): Suite/Advanced UI 和 Advanced UI工程新增加InstallScript安装包的条件检测项
  • 原文地址:https://www.cnblogs.com/kunmomo/p/11363356.html
Copyright © 2020-2023  润新知