git:git revert的更好方法,无需额外的恢复提交

Alb*_*ert 8 git revert

我在远程+本地分支中提交,我想将该提交从历史记录中删除,并将其中一些提交到自己的分支中.

基本上,我现在有:

           D---E---F---G master
Run Code Online (Sandbox Code Playgroud)

而且我要:

             E---G topic
            /
           D master
Run Code Online (Sandbox Code Playgroud)

这应该在我的本地和(只有一个,称为origin)远程存储库中.

这是最干净的方法吗?

此外,还有其他人克隆了该回购并且已经检查了主分支.如果我在远程仓库中进行这样的改变,那么'git pull'工作会让他们也进入相同的状态吗?

CB *_*ley 7

如果你已经发表了,那么你是对的,你不想重写历史master.你想要的是发布一个提交给master的提交,它将它恢复到它所处的状态,D同时保留它的当前历史记录,以便其他用户可以轻松地合并或修改他们的工作.

如果您计划在将来的某个时刻合并topicmaster那时您可能还想要做的是在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
Run Code Online (Sandbox Code Playgroud)

作为替代方案,你可以让提交E',F'和G'分别重做每个部分但是因为E,F和G已经在你发布的历史中,如果你只是引用'undo'提交并说那个,那么它可能更容易理解提交正在撤消.git revert无论如何,这就是做什么的.

基本上你所知道的就是这个.

D -- E -- F -- G -- D'      <-- master
                     \
                      \
                        G'  <-- topic
Run Code Online (Sandbox Code Playgroud)

重要的是你没有重写历史,主题是基于master的,所以合并不会意外地应用任何"撤销"提交.您现在可以安全地推都mastertopic你的远程仓库.