从历史记录中删除不是最后一个的未同步的git提交

时间:2013-01-25 19:53:37

标签: git

目前我找到的最佳解决方案是

git reset --soft HEAD^2 

保持想要的改变

git reset --mixed HEAD^

杀死不需要的提交

不幸的是,它会迫使我重新创建一个我想要消失的提交

1 个答案:

答案 0 :(得分:4)

尝试git rebase -i <target commit>^

然后,当显示要提交的提交列表时,请删除要删除的提交的行。

例如,如果我有:

6e99176 most recent commit
60dd2f0 older commit
4c82808 even older commit
b980abe commit i want to remove
5b7254b oldest commit

我能做到

git rebase -i b980abe^

并将提交:

pick 6e99176 most recent commit
pick 60dd2f0 older commit
pick 4c82808 even older commit
pick b980abe commit i want to remove

# Rebase 50ce3f5..9ad75f8 onto 50ce3f5
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

如果您按照说明操作并删除违规提交,然后保存并关闭该文件,您将从git历史记录中删除该提交。