如果你已经发表了,那么你是对的,你不想重写master
. 您想要的是向 master 发布一个提交,使其恢复到它所处的状态,D
同时保留其当前历史记录,以便其他用户可以轻松地合并或重新调整他们的工作。
如果您计划在未来的某个时候合并topic
,master
那么您可能还想做的是在 and 之间建立一个新的公共基础master
,topic
这样当您随后进行合并时topic
,您就不会丢失在 中恢复的提交master
。最简单的方法是在“撤消”提交的基础上进行“重做”提交,该提交将重置master
为其原始状态,并在此基础上建立新topic
分支。
# checkout master branch (currently at G)
git checkout master
# Reset the index to how we want master to look like
git reset D
# Move the branch pointer back to where it should be, leaving the index
# looking like D
git reset --soft HEAD@{1}
# Make a commit (D') for the head of the master branch
git commit -m "Temporarily revert E, F and G"
# Create the new topic branch based on master.
# We're going to make it on top of master and the 'undo'
# commit to ensure that subsequent merges of master->topic
# or topic->master don't merge in the undo.
git checkout -b topic
# Revert the undo commit, making a redo commit (G').
git revert HEAD
作为替代方案,您可以提交 E'、F' 和 G' 分别重做每个部分,但由于 E、F 和 G 已经在您发布的历史中,如果您只是引用“撤消”提交并说那可能更容易理解提交正在撤消。无论如何,这就是这样git revert
做的。
基本上你所知道的就是这个。
D -- E -- F -- G -- D' <-- master
\
\
G' <-- topic
重要的是您没有重写历史记录,并且主题基于 master,因此合并不会意外应用任何“撤消”提交。您现在可以安全地将两者推master
送到topic
您的远程存储库。