我在远程+本地分支中有一个提交,我想将该提交从历史记录中删除,并将其中一些放入自己的分支中。
基本上,现在我有:
D---E---F---G master
而且我要:
E---G topic
/
D master
这应该在我的本地和(只有一个,称为 origin)远程存储库中。
哪种方法最干净?
此外,还有其他人克隆了该存储库并检查了主分支。如果我在远程存储库中进行这样的更改,“git pull”是否可以让它们达到相同的状态?
如果您已经发表了,那么您不想重写历史是对的master
。您想要的是向 master 发布一个提交,使其恢复到原来的状态D
同时保留其当前历史记录,以便其他用户可以轻松合并或重新调整他们的工作。
如果您计划在未来某个时候合并topic
into master
那么你可能还想做的是在之间建立一个新的共同基础master
and 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
and topic
到您的远程存储库。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)