简体   繁体   English

如何使用 SHA git 还原提交

[英]How to git revert a commit using a SHA

How can I revert a commit with a GIVEN SHA?如何使用 GIVEN SHA 还原提交? I just want to remove the changes with a given SHA?我只想删除给定 SHA 的更改? I want to keep all the commits made BEFORE & AFTER the give SHA.我想保留在给定 SHA 之前和之后所做的所有提交。 I just want to remove changes of that specified SHA.我只想删除该指定 SHA 的更改。

I have read Revert to a commit by a SHA hash in Git?我已经阅读了通过 Git 中的 SHA 哈希恢复到提交? , my understanding is that reset all the commits made AFTER the SHA i want to revert. ,我的理解是重置所有在我想要恢复的 SHA 之后所做的提交。 That is not way i want.那不是我想要的方式。

You can use git revert <commit hash> to try to revert the changes made by the commit.您可以使用git revert <commit hash>尝试还原提交所做的更改。 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.换句话说,您将拥有历史上的第一次提交,并且在您的分支的头部有一个额外的提交,这是原始提交的有效逆向。

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 .如果您尚未与其他任何人共享您的更改,则可以使用git rebase从历史记录中完全删除原始的违规提交。 There are details in this SO post . 这篇 SO post 中有详细信息。

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 reset which 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

One can use the一个可以使用

git revert <commit hash> --no-commit 

if they want to tweak the changes produced by commit revert, before they commit again.如果他们想在再次提交之前调整提交还原产生的更改。 For more information have a look here有关更多信息,请查看此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM