如何使用 SHA git 还原提交

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

How to git revert a commit using a SHA

git

提问by michael

How can I revert a commit with a GIVEN SHA? I just want to remove the changes with a given SHA? I want to keep all the commits made BEFORE & AFTER the give SHA. I just want to remove changes of that specified SHA.

如何使用 GIVEN SHA 还原提交?我只想删除给定 SHA 的更改?我想保留在给定 SHA 之前和之后所做的所有提交。我只想删除该指定 SHA 的更改。

I have read Revert to a commit by a SHA hash in Git?, my understanding is that reset all the commits made AFTER the SHA i want to revert. That is not way i want.

我已经阅读了通过 Git 中的 SHA 哈希恢复到提交?,我的理解是重置所有在我想恢复的 SHA 之后所做的提交。那不是我想要的方式。

回答by Alex Wilson

You can use git revert <commit hash>to try to revert the changes made by the commit. This will not remove the commit from history, just make changes to undo it as a new commit. In other words you will have the first commit still in history, and an additional commit on the head of your branch which is the effective inverse of the original commit.

您可以使用git revert <commit hash>来尝试还原提交所做的更改。这不会从历史记录中删除提交,只是进行更改以将其作为新提交撤消。换句话说,您将拥有历史上的第一次提交,并且在您的分支的头部有一个额外的提交,这是原始提交的有效逆向。

If you have not yet shared your changes with anyone else, then it is possible to remove the original offending commit from history altogether by using git rebase. There are details in this SO post.

如果您尚未与其他任何人共享您的更改,则可以使用git rebase. 这篇 SO post 中有详细信息。

回答by Yuval Adam

git revert <commit>will attempt to revert a single commit.

git revert <commit>将尝试还原单个提交。

It will not change any other commits. You might be confused by git resetwhich does something entirely different.

它不会改变任何其他提交。您可能会对git reset哪个完全不同的东西感到困惑。

For more info: https://www.kernel.org/pub/software/scm/git/docs/git-revert.html

更多信息:https: //www.kernel.org/pub/software/scm/git/docs/git-revert.html

回答by Yaroslav Yakovlev

One can use the

一个可以使用

git revert <commit has> --no-commit 

if they want to tweak the changes produced by commit revert, before they commit again.

如果他们想在再次提交之前调整提交还原产生的更改。