3

I am a git newbie, I am trying to understand to undo changes in git. Say I have two branches

1: master
2: work

I am working in work branch and I want to pull the latest changes from my teammates in work branch, so I am supposed to give the command

git pull origin work

But instead, I gave the command

 git pull origin master

It fetches a bunch of commits from master branch.Now, I want to undo the last pull(assuming no local uncommited changes), will this command be sufficient

git reset --hard HEAD~1

Will the above command accomplish undoing git pull? Is there any scenario where it might not work?

Edit

After reading the answers, Do HEAD~1 and ORIG-HEAD refer to the same commit after a pull/merge?

Max
  • 9,100
  • 25
  • 72
  • 109
  • http://stackoverflow.com/questions/3998881/git-pull-into-wrong-branch – billyonecan Jul 29 '15 at 13:04
  • HEAD~1 only undoes 1 commit, okay for merges. However the pull may have added multiple commits (fast forwards). You should `git log` to see what was added. (Best is to `git tag X` before pulling). You can `git branch -f master ` if you don't want to count the pulled commits. – Kenney Jul 29 '15 at 13:06
  • It is better use `git reflog` than being looking for the git log. – blashser Jul 29 '15 at 13:07

2 Answers2

3

First of all, git reset does not take --head option, I think you mean --hard. Second, no, it's not enough as git reset --head HEAD~1 will take your repository to the state of the previous commit. If git pull resulted in many new commits, it would not be enough. You need to do:

$ git reset --hard ORIG_HEAD

It works because before doing merge, rebase and other potentially dangerous operations Git sets a special reference called ORIG_HEAD equal to the SHA1 of current HEAD before this operation. Next time before doing git pull note SHA1 of the current HEAD and after git pull see yourself that ORIG_HEAD points the previous HEAD:

$  cat .git/ORIG_HEAD
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • I have tried git pull twice one with fast forward and one with recursive. In both of the cases, git pull added one commit in git reflog, and git reset --hard HEAD~1, undid the git pull. So in what scenario will git pull result into more than one commit(in git reflog?) – Max Jul 29 '15 at 13:37
  • @Dude: did `git pull` add only commit or did it add many commits? – Arkadiusz Drabczyk Jul 29 '15 at 13:52
  • More than one commit. I can post the logs also if you want? – Max Jul 29 '15 at 17:36
  • @Dude: no, it's not necessary. In this command : `git reset --hard HEAD~1` a number after `~` means how many commits back you would like to go. You can read about it in `man gitrevisions`. If you pulled only one commit, it would work like a charm because it would bring you exactly one commit back - it's where you were before doing `git pull`. However, if you pulled multiple commits, it would bring you back to one of the commits you *pulled* and not the commit you were on before you did a `git pull`. – Arkadiusz Drabczyk Jul 29 '15 at 18:17
1

I would recommend you to use git gui and gitk to see the history and do the resets.

But if you want to use the terminal:

To see where the HEAD was before the pull

git reflog

You should see the SHA1 of the previous position of the HEAD.

Then ( As suggested by Kenney ):

git reset --hard <previousSHA1>

or:

git branch -f master <previousSHA1>
blashser
  • 921
  • 6
  • 13