我想重新定位到一个特定的提交,它不是另一个分支的 HEAD,而是倒退:
A --- B --- C master
\
\-- D --- E topic
到
A --- B --- C master
\
\-- D --- E topic
我如何以优雅和通用的方式实现这一目标?
一般而言,我的意思是目标提交 (B) 可能不一定是 HEAD 的直接祖先(我也可能会 rebase 到 A 或之前的提交),并且主题分支上可能有不止两个提交。我可能还想从 B rebase 到 A。
最佳答案
使用 git cherry-pick git rebase
是 git cherry-pick
类固醇。
如果您只有几个提交要移动:您可以使用 git cherry-pick
一一挑选
# move back to B
git checkout B
# start a branch here, and use it as the active branch
git checkout -b wip
# cherry-pick the commits you want to have
git cherry-pick D
git cherry-pick E
# if all went well : move the 'topic' branch to the current branch :
git checkout topic
git reset --hard wip
# delete the temporary 'wip' branch
git branch -d wip
使用 git rebase
如评论中所述:
git rebase
可以采取额外的选项来仅移动一系列提交。你可以做和上面一样的
cherry-pick
序列使用:git rebase --onto B master topic
额外说明: git rebase -i
git rebase
还有一个--interactive|-i
旗帜 :git rebase -i --onto B master topic
与
-i
flag :在做任何事情之前,git 会为你打开一个文本编辑器,其中将列出所有重新提交的提交。这一步的明显好处是向您展示将应用的内容(在实际应用之前),并且还描述了比“选择那个提交”更多的操作。
您可以在
git rebase -i
上搜索网络以获取更完整的教程。 ,这是一个:Git Interactive Rebase, Squash, Amend and Other Ways of Rewriting History (thoughtbot.com)
关于Git:如何 rebase 到过去的特定提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58763467/