如何从 LOG 中删除“git commit”,就像它从未存在过一样
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8901542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a 'git commit' from LOG, like it had never existed
提问by Rodrigo
I have made a mistake in one of the commits. Now I want to completely delete this commit, so it looks like it has never existed. I don't want to see this in log.
我在其中一次提交中犯了错误。现在我想完全删除这个提交,所以它看起来从未存在过。我不想在日志中看到这个。
I have tried all tips from this question ("How to delete a 'git commit'"), but I can see the commit in the log. How I can completely delete it?
我已经尝试了这个问题中的所有提示(“如何删除 'git commit'”),但我可以在日志中看到提交。我怎样才能完全删除它?
-- Edit --
- 编辑 -
Ok, I do not give the completely information. Kevin Ballardare correct.
好吧,我不提供完整的信息。凯文巴拉德是对的。
By now, I do not push this commit, it's only in my machine. The ouahanswer work, the command
到现在为止,我没有推送这个提交,它只在我的机器上。该ouah答案工作,命令
git log
will not show, but what the command
不会显示,但是命令是什么
git reset --hard HEAD^
do is "chekout last commit and change the the branch to this", so I continue seeing that commit with a graph program like SmartGit.
做的是“检查最后一次提交并将分支更改为此”,所以我继续使用像SmartGit这样的图形程序看到该提交。
--Edit 2--
--编辑2--
No, this is a SmartGit bug!!!! The commit really disappear. I have to close the windows of log and than open again. The commit is no more there.
不,这是一个 SmartGit 错误!!!!提交真的消失了。我必须关闭日志窗口,然后再打开。提交不再存在。
回答by ouah
If it is the last commit
如果是最后一次提交
git reset --hard HEAD^
if it is not the last commit
如果不是最后一次提交
git rebase -i commit_hash^
an editor will open, delete the whole line with the commit, save and quit.
将打开一个编辑器,删除带有提交的整行,保存并退出。
Note that rewriting history or rebasing if the branch has already been pushed is usually a bad idea and you may prefer to use
请注意,如果分支已经被推送,则重写历史记录或变基通常是一个坏主意,您可能更喜欢使用
git revert commit_hash
that will add a new commit that reverts the commit commit_hash
.
这将添加一个新的提交来恢复提交commit_hash
。
回答by knocte
This command (beware, it would rewrite history):
这个命令(当心,它会重写历史):
git rebase --onto commitHash^ commitHash
(@ouah's solution didn't work for me, and instead Lily's did, but his solution shouldn't be a comment, it should be an answer like this.)
(@ouah 的解决方案对我不起作用,而 Lily 的解决方案对我有用,但他的解决方案不应该是评论,而应该是这样的答案。)