我想要重基到一个特定的提交,而不是另一个分支的HEAD:
A --- B --- C master
\
\-- D topic
to
A --- B --- C master
\
\-- D topic
而不是
A --- B --- C master
\
\-- D topic
我怎样才能做到呢?
既然调整基地是如此重要,下面是内斯特·米利耶夫(Nestor Milyaev)答案的扩展。结合jsz和Adam Dymitruk回答中的Simon South的评论,得到了这个命令,无论它是从主分支的提交A还是C中分支,它都可以在topic分支上工作:
git checkout topic
git rebase --onto <commit-B> <pre-rebase-A-or-post-rebase-C-or-base-branch-name>
注意,最后一个参数是必需的(否则它会倒带分支以提交B)。
例子:
# if topic branches from master commit A:
git checkout topic
git rebase --onto <commit-B> <commit-A>
# if topic branches from master commit C:
git checkout topic
git rebase --onto <commit-B> <commit-C>
# regardless of whether topic branches from master commit A or C:
git checkout topic
git rebase --onto <commit-B> master
最后一个命令是我通常使用的命令。
我使用了上述解决方案的混合:
$ git branch temp <specific sha1>
$ git rebase --onto temp master topic
$ git branch -d temp
我发现它更容易阅读和理解。接受的解决方案导致我的合并冲突(懒得手动修复):
$ git rebase temp
First, rewinding head to replay your work on top of it...
Applying: <git comment>
Using index info to reconstruct a base tree...
M pom.xml
.git/rebase-apply/patch:10: trailing whitespace.
<some code>
.git/rebase-apply/patch:17: trailing whitespace.
<some other code>
warning: 2 lines add whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging pom.xml
CONFLICT (content): Merge conflict in pom.xml
error: Failed to merge in the changes.
Patch failed at 0001 <git comment>
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".