在不使用反向补丁的情况下从分支中删除旧的 Git 提交?

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

Remove an old Git commit from a branch without using a reverse patch?

gitgit-rebase

提问by Kit Ho

I have a master branch like this..

我有一个这样的主分支..

A -- B -- C -- D -- E -- HEAD

Is there any command that remove one of a old commit and retain the others, say commit C?

是否有任何命令可以删除旧提交之一并保留其他提交,例如提交 C?

finally it becomes like this

终于变成这样了

A -- B -- D -- E -- HEAD

I know that we can use a reverse patch and apply a new commit with reverse patch to remove commit C, but the tree structure will not be so clear and looks bulky, i.e.

我知道我们可以使用反向补丁并应用带有反向补丁的新提交来删除提交C,但是树结构不会那么清晰,看起来很笨重,即

A -- B -- C -- D -- E -- C(apply reverse patch) -- HEAD

Anyone knows?

有谁知道?

回答by Graham Borland

Use interactive rebase. For example, to go back 5 commits:

使用交互式变基。例如,要返回 5 次提交:

git rebase -i HEAD~5

Then in the editor which pops up, delete the line containing the commit you want to remove.

然后在弹出的编辑器中,删除包含要删除的提交的行。

回答by Ryan Stewart

Interactive rebase works, but to do it with just one command:

交互式 rebase 有效,但只需一个命令即可:

git rebase --onto B C

Still see the comments on the "interactive rebase" answer. They apply here, too.

仍然看到对“交互式rebase”答案的评论。它们也适用于此。