How do you undo a git pull or git merge?

Whoa so you just pulled from the master branch and lost your local changes which were committed but not pushed to repo? No problem!!

You can use the following command:

$ git reflog show 

“reflog” is used to reference logs, record when the tips of branches and other references were updated in the local repository.

Reflogs are useful in various Git commands, to specify the old value of a reference.

For example, HEAD@{2} means “where HEAD used to be two moves ago”, master@{one.week.ago} means “where master used to point to one week ago in this local repository”, and so on.

So copy the commit Id from logs and reset the repo to it using git reset

$ git reset --hard <commit-id>

…and you are back to normal, your old changes are back with this. Now you can push them so you do not lose them again. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s