删除旧的 git 提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9725156/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:12:48  来源:igfitidea点击:

Remove old git commits

git

提问by Josh

I'm very new to git, and was wondering if something like this is possible?

我对 git 很陌生,想知道这样的事情是否可能?

>git log --pretty=oneline --abbrev-commit
2f05aba Added new feature
3371cec Fixed screw up    <-- I want to remove this
daed25c Screw up          <-- and remove this.
e2b2a84 First.                So it's like they never happend.

Is this possible?

这可能吗?

采纳答案by Deve

This is possible with git rebase. Try the following

这是可能的git rebase。尝试以下

git rebase -i HEAD~4

and then, follow the interactive instructions in your editor. In the first step you "squash" the commits. It should look something like this:

然后,按照编辑器中的交互式说明进行操作。在第一步中,您“压缩”了提交。它应该是这样的:

pick 2f05aba ... will be preserved
squash 3371cec ... will be squashed with daed25c
squash daed25c ... will be squashed with e2b2a84
pick e2b2a84 .. will contain this and 3371cec and daed25c

In the second step you can edit the commit messages.

在第二步中,您可以编辑提交消息。

回答by Shathur

If you truly wish to delete them (wiping them from history, never to be seen any more), you can

如果你真的想删除它们(将它们从历史中抹去,再也见不到了),你可以

run rebase:

运行变基:

git rebase -i HEAD~4

and then, just delete (or comment out) the lines corresponding to the commits you wish to delete, like so:

然后,只需删除(或注释掉)与您要删除的提交对应的行,如下所示:

pick 2f05aba ... #will be preserved
#pick 3371cec ... #will be deleted
#pick daed25c ... #will be deleted
pick e2b2a84 ... #will be preserved

回答by Abandoned Cart

Squash is useful when you want to generate a list of merged commits under a single hash, but if you are looking to take three separate commits and have a final output that looks like it was a single commit, I recommend fixup

当您想在单个散列下生成合并提交列表时,Squash 很有用,但是如果您希望进行三个单独的提交并且最终输出看起来像是单个提交,我建议 fixup

git rebase -i HEAD~4

git rebase -i HEAD~4

pick 2f05aba ... will be preserved
fixup 3371cec ... will be merged with daed25c
fixup daed25c ... will be merged with e2b2a84
pick e2b2a84 .. will include 3371cec and daed25c, but only the commit message of e2b2a84

You can find more information in the command list of the interactive rebase UI.

您可以在交互式 rebase UI 的命令列表中找到更多信息。

回答by russoue

To completely delete commit(s) there is a new option dropfor git interactive rebases now. First run:

要完全删除提交,现在有一个drop用于 git 交互式变基的新选项。第一次运行:

git rebase -i HEAD~4

Then replace pickwith dropfor the commit(s) to be dropped:

然后替换pickdrop的提交(S)被丢弃:

pick 2f05aba ... #will be preserved
drop 3371cec ... #will be dropped
drop daed25c ... #will be dropped
pick e2b2a84 ... #will be preserved

This worked on Fedora for me with the following version:

这对我来说在 Fedora 上有以下版本:

$ git version
git version 2.21.0