Git:如何将基础重新设置为过去的特定提交



我想重新基准到一个特定的提交,它不是另一个分支的HEAD,而是向后:

A --- B --- C          master

-- D --- E   topic

A --- B --- C          master

-- D --- E   topic

我如何以优雅和一般的方式实现这一点?

一般来说,我的意思是,目标提交(B(可能不一定是HEAD的直接祖先(我还可以将基础重新设置为A或以前的提交(,并且主题分支上可能不止有两个提交。我可能还想从B调基地到A。

使用gitcherry-pick

CCD_ 1是类固醇上的CCD_ 2。

如果您只有几个提交要移动:您可以使用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标志:在执行任何操作之前,git将为您打开一个文本编辑器,其中将列出所有重新基于提交的内容。

这一步骤的明显好处是向您展示将要应用的内容(在实际应用之前(,并描述更多的操作,而不仅仅是"选择提交"。

你可以在网上搜索关于git rebase -i的更完整的教程,这里有一个:
Git Interactive Rebase,Squash,Amend and Other Ways of Rewriting History(thoughtbot.com(

相关内容

最新更新