Git 回滚 1 拉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10196668/
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
Git rollback 1 pull
提问by Bastian
I have a web server that serves a project that is a git repository. When I do some changes to the code I then do a git pull from the server. Sometimes the new code just crashes, I would like to be able to do a rollback to the latest pull, the one just before. I want to do that with a script, without having to search what is the latest sha. How can I do that?
我有一个 Web 服务器,它为一个 git 存储库项目提供服务。当我对代码进行一些更改时,我会从服务器执行 git pull 操作。有时新代码会崩溃,我希望能够回滚到最新的拉取,即之前的拉取。我想用脚本来做到这一点,而不必搜索什么是最新的 sha。我怎样才能做到这一点?
Edit: Just to clarify, I just want to have to do one action, like pushing a button that says "oops! this latest pull I just did was a mistake, I wish I didn't do that". I don't want to have to look for sha or tags or anything else in this situation, it's more like an 'undo' function. Then I want to be able to continue working on the code and the next pull on the server needs to bring the latest changes.
编辑:只是为了澄清,我只想做一个动作,比如按下一个按钮,上面写着“哎呀!我刚刚做的这个最新的拉动是一个错误,我希望我没有这样做”。我不想在这种情况下寻找 sha 或标签或其他任何东西,它更像是一个“撤消”功能。然后我希望能够继续处理代码,服务器上的下一次拉取需要带来最新的更改。
回答by Karl Bielefeldt
git reset --hard HEAD^1
will take you back one commit from what you pulled. If you want it back to the state it was in before you pulled, use git reset --hard HEAD@{1}
. The @{1}
tracks where the head was at before the last operation that changed it in your local repo, so it will go back several commits if several were pushed before you did your pull. Also see git reflog
to show the entire list.
git reset --hard HEAD^1
将从你拉取的内容中带回一次提交。如果您希望它恢复到拉动之前的状态,请使用git reset --hard HEAD@{1}
. 在@{1}
这里头是在那改变了它在当地的回购的最后一次操作之前,所以如果你没有你的拉前几个被推它会回去多次提交的轨道。另请参阅git reflog
显示整个列表。
回答by Riajul Islam
There is another way to discard last pull
还有另一种方法可以丢弃最后一次拉取
git reset --keep HEAD@{1}
回答by Fran?ois
In this case it can make sense to use a branch, which can be easily removed or merged upon failure or success. This will help you keep track of what is new, and make things cleaner if you have more complicated cases than "just remove last commit" (especially since you want to do that with a script). So on your server:
在这种情况下,使用分支是有意义的,它可以在失败或成功时轻松删除或合并。这将帮助您跟踪新内容,并在您遇到比“仅删除上次提交”更复杂的情况时使事情更清晰(尤其是因为您想使用脚本来执行此操作)。所以在你的服务器上:
git fetch --all # fetch new commits from remote
git checkout -b testing # create and switch to branch 'testing'
git merge origin/master # merge new commits from remote branch master
# (in branch 'testing')
... then test stuff... if success:
...然后测试东西...如果成功:
git checkout master # switch back to master
git merge testing
and upon failure:
失败时:
git checkout master
git branch -D testing # remove testing branch
But anyway... It your only point is to remove last commit, you can use git reset
as pointed out by Josh.
但无论如何......你唯一的一点是删除最后一次提交,你可以git reset
像Josh所指出的那样使用。
回答by Josh Farneman
回答by Jogger
The accepted answer by @Karl Bielefeldt didn't exactly worked for me. I am using GIT version 2.10.0.windows.1 May be this worked for older versions. I am getting "unknown switch 'e'" error. Finally, I did some changes and it worked.
@Karl Bielefeldt 接受的答案并不完全适合我。我正在使用 GIT 版本 2.10.0.windows.1 可能这适用于旧版本。我收到“未知开关‘e’”错误。最后,我做了一些改变,它奏效了。
Below are the steps to revert to state before previous pull:
以下是恢复到上次拉取前状态的步骤:
- Use
git reflog
to see the list as Karl mentioned. - Pick the commit version from the list to which you want to move back.
- Execute
git reset --hard <commit version>
- 使用
git reflog
查看列表卡尔提及。 - 从要移回的列表中选择提交版本。
- 执行
git reset --hard <commit version>
回答by anjaneyulubatta505
I've used below command to revert the last commit
我使用下面的命令来恢复最后一次提交
git merge --abort
回答by omkar
There are two commands :
有两个命令:
git reset --hard@{1}
git reset --hard^1
git reset --hard@{1}
git reset --hard^1
First : rolls back to state before last pull.
First :回滚到上次拉取之前的状态。
second: rolls back to state before last commit ( includes merged code ).
第二:回滚到上次提交之前的状态(包括合并代码)。
Hope it helps.
希望能帮助到你。
回答by Greg Demetrick
Another way of doing this would involve using tags. The thought being you could tag your HEAD with some version number prior to the pull, do the pull, then if you need to roll the HEAD back you can do git reset {tag name}
That way if the pull has multiple commits you can skip back to where you were prior to the merge.
另一种方法是使用标签。想法是你可以在 pull 之前用一些版本号标记你的 HEAD,做 pull,然后如果你需要回滚 HEAD,你可以这样做git reset {tag name}
如果 pull 有多个提交,你可以跳回到你之前的位置到合并。