如何使用 on 命令 git rebase 一个分支? [英] How to git rebase a branch with the onto command?

查看:38
本文介绍了如何使用 on 命令 git rebase 一个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have noticed that the two blocks of following git commands have different behaviours and I don't understand why.

我有一个 A 和一个 B 分支,它们与一个 commit

I have an A and a B branches that diverge with one commit

---COMMIT--- (A)

 --- (B)

我想在最新的 A 上重新设置 B 分支(并在 B 分支上提交)

I want to rebase B branch on the lastest A (and have the commit on the B branch)

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

如果我这样做没问题:

checkout B
rebase A

但如果我这样做:

checkout B
rebase --onto B A

它根本不起作用,什么也没有发生.我不明白为什么这两种行为不同.

It doesn't work at all, nothing happens. I don't understand why the two behaviours are different.

PhpStorm GIT 客户端使用第二种语法,所以似乎完全被破坏了,这就是我问这个语法问题的原因.

PhpStorm GIT client uses the second syntax, and so seems to be completely broken, that's why I ask for this syntax issue.

推荐答案

tl;dr

在您的情况下使用 git rebase --ontoA 之上重新定位 B 的正确语法是:

tl;dr

The correct syntax to rebase B on top of A using git rebase --onto in your case is:

git checkout B
git rebase --onto A B^

A 之上重新定位 B,从作为 B 的父级的提交开始B^B~1.

or rebase B on top of A starting from the commit that is the parent of B referenced with B^ or B~1.

如果您对 git rebase git rebase --onto 之间的区别感兴趣,请继续阅读.

If you're interested in the difference between git rebase <branch> and git rebase --onto <branch> read on.

git rebase <branch> 将在 最新提交的基础上对您当前已检出的分支进行 rebase,由 HEAD 引用可从 访问,但 不能HEAD 访问.
这是最常见的变基案例,可以说是需要较少预先计划的案例.

git rebase <branch> is going to rebase the branch you currently have checked out, referenced by HEAD, on top of the latest commit that is reachable from <branch> but not from HEAD.
This is the most common case of rebasing and arguably the one that requires less planning up front.

          Before                           After
    A---B---C---F---G (branch)        A---B---C---F---G (branch)
                                                      
              D---E (HEAD)                              D---E (HEAD)

在此示例中,FG 是可从 branch 访问但不能从 HEAD 访问的提交.说 git rebase branch 将采用 D,即分支点之后的第一次提交,然后 rebase it(即 更改其父) 位于 branch 的最新提交之上,但不是来自 HEAD,即 G.

In this example, F and G are commits that are reachable from branch but not from HEAD. Saying git rebase branch will take D, that is the first commit after the branching point, and rebase it (i.e. change its parent) on top of the latest commit reachable from branch but not from HEAD, that is G.

git rebase --onto 允许你从一个特定的提交开始变基.它使您可以精确控制正在重新定位的内容和位置.这适用于需要精确的场景.

git rebase --onto allows you to rebase starting from a specific commit. It grants you exact control over what is being rebased and where. This is for scenarios where you need to be precise.

例如,假设我们需要从 E 开始在 F 之上精确地将 HEAD 变基.我们只对将 F 引入我们的工作分支感兴趣,同时,我们不想保留 D,因为它包含一些不兼容的更改.>

For example, let's imagine that we need to rebase HEAD precisely on top of F starting from E. We're only interested in bringing F into our working branch while, at the same time, we don't want to keep D because it contains some incompatible changes.

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

在这种情况下,我们会说 git rebase --onto F D.这意味着:

In this case, we would say git rebase --onto F D. This means:

从父为 DHEADF 之上重新建立可访问的提交.

Rebase the commit reachable from HEAD whose parent is D on top of F.

换句话说,E的父D改为F.git rebase --onto 的语法是 git rebase --onto <oldparent>.

In other words, change the parent of E from D to F. The syntax of git rebase --onto is then git rebase --onto <newparent> <oldparent>.

另一种派上用场的场景是,您想快速从当前分支中删除一些提交,而无需执行交互式变基:

Another scenario where this comes in handy is when you want to quickly remove some commits from the current branch without having to do an interactive rebase:

          Before                       After
    A---B---C---E---F (HEAD)        A---B---F (HEAD)

在这个例子中,为了从序列中删除 CE,你可以说 git rebase --onto BE,或者 rebaseHEAD 位于 B 之上,旧的父节点是 E.

In this example, in order to remove C and E from the sequence you would say git rebase --onto B E, or rebase HEAD on top of B where the old parent was E.

git rebase --onto 在精度方面可以更进一步.事实上,它允许您将任意范围的提交重新绑定到另一个提交之上.

git rebase --onto can go one step further in terms of precision. In fact, it allows you to rebase an arbitrary range of commits on top of another one.

这是一个例子:

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

在这种情况下,我们希望将 E---H 的确切范围重新定位在 F 之上,忽略 HEAD 当前所在的位置指向.我们可以通过说 git rebase --onto F D H 来做到这一点,这意味着:

In this case, we want to rebase the exact range E---H on top of F, ignoring where HEAD is currently pointing to. We can do that by saying git rebase --onto F D H, which means:

F 之上将父级为 D 的提交范围重新设置为 H.

Rebase the range of commits whose parent is D up to H on top of F.

git rebase --onto 的语法与提交范围 然后变成 git rebase --onto <newparent><老父母><直到>.这里的技巧是记住 <until> 引用的提交被 included 在范围内,并且在 rebase 之后将成为新的 HEAD完全的.

The syntax of git rebase --onto with a range of commits then becomes git rebase --onto <newparent> <oldparent> <until>. The trick here is remembering that the commit referenced by <until> is included in the range and will become the new HEAD after the rebase is complete.

这篇关于如何使用 on 命令 git rebase 一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆