我注意到以下git命令的两个块有不同的行为,我不明白为什么。

我有一个A和一个B分支,它们在一次提交中分离

---COMMIT--- (A)
\
 --- (B)

我想在最近的A上重新建立B分支(并在B分支上提交)

---COMMIT--- (A)
         \
          --- (B)

如果我这样做没有问题:

checkout B
rebase A

但如果我这样做了:

checkout B
rebase --onto B A

根本不管用,什么都没发生。我不明白为什么这两种行为不同。

PhpStorm GIT客户端使用第二种语法,所以看起来完全被破坏了,这就是为什么我问这个语法问题。


当前回答

简单地说,git rebase——onto选择了一系列提交,并根据参数给出的commit对它们进行了重基。

阅读git rebase的手册,搜索“onto”。下面的例子非常好:

example of --onto option is to rebase part of a branch. If we have the following situation:

                                   H---I---J topicB
                                  /
                         E---F---G  topicA
                        /
           A---B---C---D  master

   then the command

       git rebase --onto master topicA topicB

   would result in:

                        H'--I'--J'  topicB
                       /
                       | E---F---G  topicA
                       |/
           A---B---C---D  master

在这种情况下,您告诉git将提交从topicA重置为topicB,位于master之上。

其他回答

为了更好地理解git rebase和git rebase之间的区别,最好了解这两个命令可能的行为。Git rebase允许我们将提交移到所选分支的顶部。像在这里:

git rebase master

结果是:

Before                              After
A---B---C---F---G (master)          A---B---C---F---G (master)
         \                                           \
          D---E (HEAD next-feature)                   D'---E' (HEAD next-feature)

Git rebase -onto更精确。它允许我们选择特定的提交,我们想从哪里开始,也想在哪里结束。像在这里:

git rebase --onto F D

结果是:

Before                                    After
A---B---C---F---G (branch)                A---B---C---F---G (branch)
         \                                             \
          D---E---H---I (HEAD my-branch)                E'---H'---I' (HEAD my-branch)

要获得更多详细信息,我建议您查看我自己关于git rebase的文章—进入概述

这就是你需要了解的全部内容——onto:

git rebase --onto <newparent> <oldparent>

您在提交时切换了父节点,但您没有提供提交的sha,只提供它当前(旧)父节点的sha。

Git的措辞在这里有点令人困惑。如果你把命令伪装成这样可能会有所帮助:

Git rebase - to=<new_base> <old_base>[<分支>]

如果我们现在在分支上,它可以被省略:

git rebase - access =<new_base> <old_base>

如果new_base和old_base相同,我们可以省略——onto形参:

Git rebase <new_old_base>

这可能听起来很奇怪:如果旧的基数和新的基数相同,您如何重设基数?但是可以这样想,如果你有一个功能分支foo,它已经(可能)基于你的主分支中的一些提交。通过“re- based”,我们只是让它基于的提交更当前。

(In fact, <old_base> is something that we compare branch against. If it's a branch, then git looks for a common ancestor (see also --fork-point); if it's a commit on current branch, the commits after that are used; if it's a commit that has no common ancestor with current branch, all commits from current branch are used. <new_base> can also be a commit. So, for instance, git rebase --onto HEAD~ HEAD will take commits between old base HEAD and current HEAD and place them on top of HEAD~, effectively deleting the last commit.)

你还需要另外两个分支。通过这个命令,你可以将来自branchB的基于branchA的提交应用到另一个分支,例如master。在下例中,branchB基于branchA,您希望在master上应用branchB的更改,而不应用branchA的更改。

o---o (master)
     \
      o---o---o---o (branchA)
                   \
                    o---o (branchB)

使用命令:

checkout branchB
rebase --onto master branchA 

您将得到以下提交层次结构。

      o'---o' (branchB)
     /
o---o (master)
     \
      o---o---o---o (branchA)

还有另外一个git rebase -onto很难理解的情况:当你rebase到一个对称差分选择器(三个点'…')导致的提交上时

Git 2.24(2019年Q4)在管理这种情况方面做得更好:

See commit 414d924, commit 4effc5b, commit c0efb4c, commit 2b318aa (27 Aug 2019), and commit 793ac7e, commit 359eceb (25 Aug 2019) by Denton Liu (Denton-L). Helped-by: Eric Sunshine (sunshineco), Junio C Hamano (gitster), Ævar Arnfjörð Bjarmason (avar), and Johannes Schindelin (dscho). See commit 6330209, commit c9efc21 (27 Aug 2019), and commit 4336d36 (25 Aug 2019) by Ævar Arnfjörð Bjarmason (avar). Helped-by: Eric Sunshine (sunshineco), Junio C Hamano (gitster), Ævar Arnfjörð Bjarmason (avar), and Johannes Schindelin (dscho). (Merged by Junio C Hamano -- gitster -- in commit 640f9cd, 30 Sep 2019)

Rebase:快进——更多情况下为上 之前,当我们有下面的图表时, A - B - C(主) \ D(侧) 运行'git rebase - to master…master side'将导致D总是被重基,无论如何。

在这一点上,阅读“双点之间的区别是什么?”'和三点"…"在Git diff提交范围?"

:“主人…“指的是大师……HEAD,也就是B: HEAD是侧HEAD(目前签出):您正在重新基于B。 你在改变什么基础?任何不在master中的提交,并且从side branch可以到达:只有一个提交符合描述:D…已经在B上面了!

同样,在Git 2.24之前,这样的rebase -onto会导致D总是被重基,无论发生什么。

然而,期望的行为是,rebase应该注意到这是快速前进的,并取而代之。

这类似于OP的B - A的重基,而OP什么也没做。

Add detection to can_fast_forward so that this case can be detected and a fast-forward will be performed. First of all, rewrite the function to use gotos which simplifies the logic. Next, since the options.upstream && !oidcmp(&options.upstream->object.oid, &options.onto->object.oid) conditions were removed in cmd_rebase, we reintroduce a substitute in can_fast_forward. In particular, checking the merge bases of upstream and head fixes a failing case in t3416. The abbreviated graph for t3416 is as follows:

        F---G topic
       /
  A---B---C---D---E master

失败的命令是

git rebase --onto master...topic F topic

在此之前,Git会看到有一个merge base (C, result of master…topic),并且merge和onto是相同的,因此它会错误地返回1 我们可以快进。这将导致重基图为“ABCFG” 当时我们在期待《ABCG》。

一个rebase—to C F topic意味着在F之后的任何提交,可以通过topic HEAD到达:这只是G,而不是F本身。 在这种情况下,快进会将F包含在重基分支中,这是错误的。

使用附加的逻辑,我们检测上游和头部的合并基 是F,因为onto不是F,这意味着我们没有重基 从master..topic提交。 因为我们排除了一些提交,所以不能执行快进,所以我们正确地返回0。 将'-f'添加到由于此更改而失败的测试用例中,因为 他们并没有料到会有这么快的进展,以至于被迫调降基数。