简体   繁体   English

git:删除第二次提交

[英]git: remove 2nd commit

I'm trying to remove the 2nd commit to a repo. 我正在尝试删除第二次提交到回购。 At this point I could just blow away the .git dir and re-do it, but I'm curious how to do this... I've deleted commits before, but apparently never the 2nd one :) 在这一点上,我可以吹掉.git目录并重新执行它,但我很好奇如何做到这一点...我之前删除了提交,但显然从来没有第二个:)

> git log

commit c39019e4b08497406c53ceb532f99801793205ca
Author: Me
Date:   Thu Mar 22 14:02:41 2012 -0700

    Initializing registry directories

commit 535dce28f1c68e8af9d22bc653aca426fb7825d8
Author: Me
Date:   Tue Jan 31 21:04:13 2012 -0800

    First Commit

> git rebase -i HEAD~2
fatal: Needed a single revision
invalid upstream HEAD~2

> git rebase -i HEAD~1

at which point I get in my editor: 在这一点上,我进入我的编辑器:

pick c39019e Initializing registry directories

# Rebase 535dce2..c39019e onto 535dce2
#
# 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.
#

Now my problem is that I can't just blow away this 2nd commit since "if you remove everything, the rebase will be aborted" 现在我的问题是,我不能只是吹掉第二次提交,因为“如果你删除所有内容,那么rebase将被中止”

In order to remove the topmost commit, use git reset --hard HEAD~ . 要删除最顶层的提交,请使用git reset --hard HEAD~ Rebase is not needed since you are not removing anything in between other commits. 不需要Rebase,因为您没有在其他提交之间删除任何内容。

This is already answered (above) but note that in newer git, there is a noop command you can put in the file. 这已经回答了(上面),但请注意,在较新的git中,有一个noop命令可以放入文件中。 So you can replace the pick line with noop : 所以你可以用noop替换pick线:

$ git rebase -i HEAD^
[in editor, change pick line to noop, and write and quit]
".git/rebase-merge/git-rebase-todo" 15L, 492C written
Successfully rebased and updated refs/heads/master.
$ 

Admittedly this does nothing that git reset does not do just as easily ... but if you've already started the interactive rebase, and you only realize what you wanted after the fact, the noop trick is handy. 不可否认, git reset并不能轻易做到这一点......但是如果你已经启动了交互式rebase,并且你只是意识到你想要的事情,那么noop技巧就很方便了。

Why dont you revert your commit? 为什么不还原你的提交?

git revert 535dce28f1c68e8af9d22bc653aca426fb7825d8 or git revert HEAD~1 git revert 535dce28f1c68e8af9d22bc653aca426fb7825d8git revert HEAD~1

You can also do the following: 您还可以执行以下操作:

git rebase -i --root

This will include the root commit in your rebase. 这将包括您的rebase中的root提交。 You can then choose to fixup , squash , or delete that 2nd commit entirely if you desire. 如果您愿意,您可以选择完全fixupsquash或删除第二次提交。

If it is the last commit, then you can do: 如果是最后一次提交,那么你可以这样做:

git reset --hard HEAD~

And if it is not the last commit, you would have had another commit atleast in the rebase list and you can remove the 2nd commit. 如果它不是最后一次提交,你将在rebase列表中至少进行另一次提交,你可以删除第二次提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM