如何在 git 中撤销提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5381945/
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
How do I reverse a commit in git?
提问by David Mulder
I'm not really familiar with how git works. I pushed a commit by mistake and want to revert it. I did a
我不太熟悉 git 的工作原理。我错误地推送了一个提交并想恢复它。我做了一个
git reset --hard HEAD~1
Beware Fellow Googlers:This does not only revert the commit, but discards all file changes!
当心 Google 员工:这不仅会还原提交,还会丢弃所有文件更改!
and now the project is reverted on my machine, but not on github. If I try to push this code, I get the error 'Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.' How do I remove this commit from github?
现在该项目已在我的机器上恢复,但不在 github 上。如果我尝试推送此代码,则会收到错误消息“您的分支在 1 次提交之前落后于 'origin/master',并且可以快进。” 如何从 github 中删除此提交?
采纳答案by ?imon Tóth
You can do git push --force
but be aware that you are rewriting history and anyone using the repo will have issue with this.
您可以这样做,git push --force
但请注意,您正在重写历史记录,任何使用该存储库的人都会遇到此问题。
If you want to prevent this problem, don't use reset, but instead use git revert
如果你想防止这个问题,不要使用重置,而是使用 git revert
回答by AmpT
This articlehas an excellent explanationas to how to go about various scenarios(where a commit has been done as well as the push OR just a commit, before the push):
该文章有一个极好的说明为如何去了解各种情况(如提交已经完成以及推或只是一个承诺,推前):
http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html
http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html
From the article, the easiest commandI saw to revert a previous commit by its commit id, was:
从文章中,我看到通过提交 id 恢复先前提交的最简单命令是:
git revert dd61ab32
回答by Luke
Or you can try using git revert http://www.kernel.org/pub/software/scm/git/docs/git-revert.html. I think something like git revert HEAD~1 -m 1
will revert your last commit (if it's still the last commit).
或者您可以尝试使用 git revert http://www.kernel.org/pub/software/scm/git/docs/git-revert.html。我认为类似的东西git revert HEAD~1 -m 1
会恢复你的最后一次提交(如果它仍然是最后一次提交)。
回答by Rodel30
Unable to comment on others answers, I'll provide a bit of extra information.
无法评论其他人的答案,我将提供一些额外的信息。
If you want to revert
the last commit, you can use git revert head
. head
refers to the most recent commit in your branch.
如果你想revert
最后一次提交,你可以使用git revert head
. head
指的是您分支中的最新提交。
The reason you use head~1
when using reset
is that you are telling Git to "remove all changes in the commits after" (reset --hard
) "the commit one before head" (head~1
).
您在使用head~1
时使用的原因reset
是您告诉 Git“在提交之后删除所有更改”(reset --hard
)“在提交之前的提交”(head~1
)。
reset
is toa commit, revert
is ona commit.
reset
是要提交,revert
是上提交。
As AmpT pointed out, you can also use the commit SHA to identify it, rather than counting how far away from head
it is. The SHA can be found in the logs (git log
) and a variety of other ways.
正如 AmpT 所指出的,您还可以使用提交 SHA 来识别它,而不是计算离head
它有多远。可以在日志 ( git log
) 和各种其他方式中找到 SHA 。
You can also always use any other pointers in Git. e.g. a tag or branch. And can also use all of these fun other ways to reference commits https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html#_specifying_revisions
您也可以随时使用 Git 中的任何其他指针。例如标签或分支。并且还可以使用所有这些有趣的其他方式来引用提交https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html#_specifying_revisions
回答by Ilkka
I think you need to push a revert commit. So pull
from github again, including the commit you want to revert, then use git revert
and push the result.
我认为您需要推动恢复提交。所以pull
再次从github,包括你想要恢复的提交,然后使用git revert
并推送结果。
If you don't care about other people's clones of your github repository being broken, you can also delete and recreate the master branch on github after your reset
: git push origin :master
.
如果你不关心别人克隆你的 github 仓库被破坏,你也可以在你的reset
:之后删除并重新创建 github 上的 master 分支git push origin :master
。
回答by jho
git push -f
maybe?
git push -f
也许?
man git-push
will tell more.
man git-push
会告诉更多。