一个分支如何既比X个提交超前又比Y个提交落后呢?

4
我在 Github 上检查一些项目时,发现其中一个项目的代码中出现以下提示:This branch is 287 commits ahead, 361 commits behind X:Master。这是如何可能的呢?针对这个问题,我需要进一步了解有关该项目的背景信息才能给予更准确的回答。
2个回答

6
就像这样:
       o ----- ... 285 commits ... -- o     <-- master
      /
o -- o -- o -- ... 359 commits ... -- o     <-- X/master

您在本地存储库的主分支上创建了287个提交。这些提交无法从X/master访问。这就是为什么master会显示“287个提交领先于X/master”的原因。
与此同时,其他开发人员在X/master上创建了361个新的提交。自从本地主分支和X/master分离以来,这361个提交无法从master访问,这就是为什么master会显示“361个提交落后于X/master”的原因。
这是版本控制系统中的正常情况。您可以通过两种方式将主分支与X/master同步:
  1. merge

    Run:

    git checkout master
    git merge X/master
    

    This creates a new commit that contains all the changes from both master and X/master and has two parents: the current tips of the master and X/master branches.

    If it succeeds, the graph looks like this:

                                       +--- old "master"  
                                       v
           o -- ... 285 commits ... -- o -- o   <-- master (the merge commit)
          /                                /
    o -- o -- o -- ... 359 commits ... -- o     <-- X/master
    
  2. rebase

    Run:

    git checkout master
    git rebase X/master
    

    This moves all the commits that are on master but not on X/master (the 287 commits that are "ahead") on the X/master branch, after the commits 361 that are on X/master and are not accesible from master.

    If it succeeds, the graph will look like this:

         +--- old split point
         v
    o -- o - ... 360 commits ... - o - ... 286 commits ... - o   <-- master
                                   ^
                                   +---- X/master
    

实际上,由于有如此多不同的提交,似乎很可能其他人已经执行过rebase操作了。OP应该注意,rebase的工作方式是通过“复制”提交来完成的。如果你和他们有287个共享提交,并且他们有74个未共享的新提交,而“他们”将他们的287个提交重新基于新的副本,他们会突然拥有287 + 74 = 361个你没有的提交,你会有361 - 74 = 287个原始(复制之前的)提交。 - torek

2

非常简单:

  • 在X时间从主分支创建了该分支
  • 对分支进行了更改并将更改推送到服务器
  • 对主分支进行了更改并将更改推送到服务器

这时该分支的更改领先于主分支,但其也落后于已经推送到主分支的更改。它们需要重新基于或合并以进行组合。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接