我想要重基到一个特定的提交,而不是另一个分支的HEAD:
A --- B --- C master
\
\-- D topic
to
A --- B --- C master
\
\-- D topic
而不是
A --- B --- C master
\
\-- D topic
我怎样才能做到呢?
添加到答案中使用——onto:
我从来没有把它背下来,所以我写了这个小助手脚本:
Git:将一个分支(子)从一个基基重设为另一个基基,留下另一个基基的提交。
用法:
moveBranch <branch> from <previous-base> to <new-base>
简而言之:
git rebase --onto "$3" "$2" "$1"
除此之外,还有一个类似的解决方案,那就是选择一系列的提交:
git co <new-base>
git cherry-pick <previous-base>..<branch>
git branch -f branch
两者的效果差不多。请注意,此语法跳过<previous-branch>本身的提交,因此它会优选下一个和后续的提交,包括<branch>的提交。
我使用了上述解决方案的混合:
$ 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".