Is 'git reset - hard Head ~ 1' enough to undo a git pull?
I am a git newbie, I am trying to figure out how to revert changes in git. Let's say I have two branches
1: master
2: work
I am on a working branch and I want to pull the latest changes from my teammates on the working branch, so I have to give the command
git pull origin work
But instead I gave the command
git pull origin master
It checks out a bunch of commits from the master branch. Now I want to undo the last click (with no local unmanaged changes), will this command be sufficient
git reset --hard HEAD~1
Will the above command do a git pull undo? Is there a scenario where it might not work?
Edit
After reading the answers Do HEAD ~ 1 and ORIG-HEAD refer to the same commit after pull / merge?
source to share
First of all, git reset
doesn't take a parameter --head
, I think you mean --hard
. Secondly, no, this is not enough, as it will git reset --head HEAD~1
move your repository to the state of the previous commit. If it git pull
led to many new commits, it wouldn't be enough. You need to do:
$ git reset --hard ORIG_HEAD
This works because before performing a merge, rebase, and other potentially dangerous Git
operations, a special link named ORIG_HEAD
equal to the SHA1
current HEAD is set before the operation . Next time, before doing, git pull
pay attention to the SHA1
current one HEAD
and after git pull
make sure that ORIG_HEAD
the previous one indicates HEAD
:
$ cat .git/ORIG_HEAD
source to share
I would recommend that you use git gui
and gitk
to view your history and perform a reset.
But if you want to use terminal:
To see where HEAD is, before pressing
git reflog
You should see the SHA1 of the previous HEAD position.
Then (as Kenny suggested):
git reset --hard <previousSHA1>
or
git branch -f master <previousSHA1>
source to share