Krz*_*iak 5 git version-control git-rebase
我想变基到一个特定的提交,该提交不是另一个分支的 HEAD,而是向后移动:
A --- B --- C master
\
\-- D --- E topic
Run Code Online (Sandbox Code Playgroud)
到
A --- B --- C master
\
\-- D --- E topic
Run Code Online (Sandbox Code Playgroud)
我如何以优雅且通用的方式实现这一目标?
一般来说,我的意思是目标提交(B)不一定是 HEAD 的直接祖先(我也可能变基到 A 或先前的提交),并且主题分支上可能不止两个提交。我可能还想从 B 变基到 A。
使用 gitcherrypick
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
Run Code Online (Sandbox Code Playgroud)
使用 git rebase
正如评论中提到的:git rebase可以采取额外的选项来仅移动一系列提交。
您可以使用以下命令执行与上述序列相同的操作cherry-pick:
git rebase --onto B master topic
Run Code Online (Sandbox Code Playgroud)
额外说明:git rebase -i
git rebase还有一个--interactive|-i标志:
git rebase -i --onto B master topic
Run Code Online (Sandbox Code Playgroud)
使用-i标志 : 在执行任何操作之前,git 将为您打开一个文本编辑器,其中将列出所有重新调整的提交。
此步骤的明显好处是向您展示将要应用的内容(在实际应用之前),并且还描述了不仅仅是“选择该提交”的更多操作。
您可以在网上搜索更完整的教程git rebase -i,这里有一个:
Git Interactive Rebase, Squash, Amend and Other Ways of Rewriting History (thoughtbot.com)
| 归档时间: |
|
| 查看次数: |
14055 次 |
| 最近记录: |