简体   繁体   English

如何在git中返回以前的版本

[英]How to go back to previous version in git

I have a checkout copy of a remote git repository in my workstation. 我的工作站中有一个远程git存储库的签出副本。 I have accidentally committed a change in my local and pushed to remote. 我不小心在本地进行了更改,然后将其推送到远程。 Now I want to remove the last commit and go back to the previous commit and the same should be pushed to remote. 现在,我想删除最后一个提交并返回到上一个提交,并且应该将其推送到远程。

I am okay if it is a new commit with a commit message. 如果这是带有提交消息的新提交,我可以。 How to do that? 怎么做?

If nobody has yet cloned your updated remote repo, you can: 如果没有人克隆您更新的远程仓库,则可以:

git reset --hard HEAD~
git push --force

That will force the local and remote history to discard the latest commit. 这将迫使本地和远程历史记录放弃最新的提交。
(Check first with a git fetch that no new commits had been pushed since your incorrect commit) (首先用git fetch检查,因为您的错误提交,没有新的提交被推送)

If a new history isn't an option, then a git revert is more secure, and will result in a new commit canceling the previous one: you can then push that new commit. 如果没有新的历史记录,那么git revert会更安全,并且会导致新的提交取消先前的提交:然后可以推送该新提交。

I'd advise against pushing with --force an alternative history. 我建议不要用--force推动替代历史。 Anyone who already pulled your changes will have a completely screwed history to deal with when pulling new stuff. 任何已经撤消您的更改的人在撤消新内容时都将拥有完全错误的历史记录。

A far safer option is to simply do 一个更安全的选择是简单地

git revert HEAD~1 
git push origin master

git revert will record a new commit that cancels all of the effects of the previous one git revert将记录一个新的提交,该提交将取消上一个的所有影响

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

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