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

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

How to go back to previous version in git

git

提问by pooamlairaj

I have a checkout copy of a remote git repository in my workstation. 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.

我的工作站中有一个远程 git 存储库的结帐副本。我不小心在本地进行了更改并推送到远程。现在我想删除最后一次提交并返回上一次提交,并且应该将其推送到远程。

I am okay if it is a new commit with a commit message. How to do that?

如果这是一个带有提交消息的新提交,我没问题。怎么做?

采纳答案by Tigraine

I'd advise against pushing with --force an alternative history. Anyone who already pulled your changes will have a completely screwed history to deal with when pulling new stuff.

我建议不要用 --force 推动替代历史。任何已经拉动你的更改的人在拉动新东西时都会有一个完全搞砸的历史。

A far safer option is to simply do

一个更安全的选择是简单地做

git revert HEAD~1 
git push origin master

git revertwill record a new commit that cancels all of the effects of the previous one

git revert将记录一个新的提交,该提交取消前一个提交的所有影响

回答by VonC

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 revertis more secure, and will result in a new commit canceling the previous one: you can then push that new commit.

如果一个新的历史不是一个选项,那么 agit revert更安全,并且会导致一个新的提交取消前一个:然后你可以推送新的提交。