如何使用on命令在分支上使用git rebase? [英] How to git rebase a branch with the onto command?
问题描述
我注意到以下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 --onto
在 A
上将 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
顶部的
或 rebase 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< branch>
和 git rebase --onto< branch>
之间的区别感兴趣,请继续阅读.
If you're interested in the difference between git rebase <branch>
and git rebase --onto <branch>
read on.
git rebase< branch>
将在最新提交的基础上,将当前已检出的分支(由 HEAD
引用)重新建立基础从< branch>
可以访问,但从 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)
在此示例中, F
和 G
是可以从 branch
而不是 HEAD
到达的提交.说 git rebase分支
将采用 D
,这是分支点之后的第一个提交,然后对其重新设置(即更改其父代))位于可以从 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
开始将 HEAD
精确地重新设置在 F
的基础上.我们只想将 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 --on F D
.这意味着:
In this case, we would say git rebase --onto F D
. This means:
将父代为
D
的HEAD
可以到达的提交重新设置为基准,位于F
之上.
Rebase the commit reachable from
HEAD
whose parent isD
on top ofF
.
换句话说,将 E
的父代从 D
更改为 F
.然后 git rebase --onto
的语法为 git rebase --onto< newparent>.< 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)
在此示例中,为了从序列中删除 C
和 E
,您可以说 git rebase --onto BE
或重新设置基础 B
顶部的 HEAD
,其中旧父母是 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)
在这种情况下,我们要在 F
的基础上将准确的范围 E --- H
重新建立基础,而忽略 HEAD
当前所在的位置指向.我们可以通过说 git rebase --on 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:
将父代为
D
的提交范围重新设置为F
之上的H
.
Rebase the range of commits whose parent is
D
up toH
on top ofF
.
具有提交范围的 git rebase --onto
的语法随后变为 git rebase --onto< newparent>.< oldparent><直到>
.这里的技巧是记住< until>
引用的提交在该范围内 included ,并且在重新设置基准后将成为新的 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屋!