如何从 Git 中删除特定提交?

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

How to remove specific commits from Git?

git

提问by ANIL

I want to remove few commits from my remote repository. I have few commits like this in my repo:

我想从我的远程存储库中删除一些提交。我的回购中很少有这样的提交:

commits    messages

abcd123    some message
xyze456    another commit message
98y65r4    this commit is not required
987xcrt    this commit is not required
bl8976t    initial commit

I want to remove commit 98y65r4and 987xcrtfrom my repo. How to achieve it?

我想从我的回购中删除提交 98y65r4987xcrt。如何实现?

回答by MondKin

There are two alternatives to this: the safe one that leaves you with a dirty git history, or the unsafe one that leaves you with a clean git history. You pick:

对此有两种选择:安全的一种让您拥有肮脏的 git 历史记录,或者不安全的一种让您拥有干净的 git 历史记录。你选:

Option 1: Revert

选项 1:还原

You can tell git to "Revert a commit". This means it will introduce a change that reverts each change you made in a commit. You would need to execute it twice (once per commit):

您可以告诉 git“还原提交”。这意味着它将引入一个更改,该更改将还原您在提交中所做的每个更改。您需要执行两次(每次提交一次):

git revert 98y65r4
git revert 987xcrt

This solution will leave your git history like this (you can execute gitk --allto see a graphical representation of the state of your repo):

此解决方案将像这样保留您的 git 历史记录(您可以执行gitk --all以查看存储库状态的图形表示):

2222222    revert of 987xcrt: this commit is not required
1111111    revert of 98y65r4: this commit is not required
abcd123    some message
xyze456    another commit message
98y65r4    this commit is not required
987xcrt    this commit is not required
bl8976t    initial commit

Then you can push the new 2 commits to your remote repo:

然后你可以将新的 2 个提交推送到你的远程仓库:

git push

This solution is safe because it does not make destructive operations on your remote repo.

此解决方案是安全的,因为它不会对您的远程存储库进行破坏性操作。

Option 2: Interactive rebase

选项 2:交互式变基

You also can use an interactive rebase for that. The command is:

您也可以为此使用交互式变基。命令是:

git rebase -i bl8976t

In it, you are telling git to let you select which commits you want to mix together, reorder or remove.

在其中,您告诉 git 让您选择要混合、重新排序或删除的提交。

When you execute the command an editor will open with a text similar to this:

当您执行该命令时,一个编辑器将打开,并显示类似于以下内容的文本:

pick    bl8976t    initial commit
pick    987xcrt    this commit is not required
pick    98y65r4    this commit is not required
pick    xyze456    another commit message
pick    abcd123    some message

Just go on and delete the lines you don't want, like this:

继续删除不需要的行,如下所示:

pick    bl8976t    initial commit
pick    xyze456    another commit message
pick    abcd123    some message

Save the file and close the editor.

保存文件并关闭编辑器。

So far, this has only modified the local copy of your repository (you can see the tree of commits with gitk --all).

到目前为止,这仅修改了存储库的本地副本(您可以使用 看到提交树gitk --all)。

Now you need to push your changes to your repo, which is done with a "push force", but before you execute the command bear in mind that push force is a destructive operation, it will overwrite the history of your remote repository and can cause merge troubles to other people working on it. If you are ok and want to do the push force, the command is:

现在您需要将更改推送到您的存储库,这是通过“推力”完成的,但在执行命令之前请记住推力是一种破坏性操作,它会覆盖远程存储库的历史记录并可能导致将麻烦合并到其他人的工作中。如果你没问题并且想做推力,命令是:

git push -f