Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'git reset --hard Head~1' enough to undo a git pull?

Tags:

git

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?

like image 854
Dude Avatar asked Jun 07 '26 16:06

Dude


1 Answers

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
like image 131
Arkadiusz Drabczyk Avatar answered Jun 09 '26 08:06

Arkadiusz Drabczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!