Skip to content

Instantly share code, notes, and snippets.

@shiftkey
Created January 9, 2014 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiftkey/8343015 to your computer and use it in GitHub Desktop.
Save shiftkey/8343015 to your computer and use it in GitHub Desktop.
Git: reverting a revert using the reflog

Recovering from a bad revert

Open up Git Shell for your repository and type this command:

git reflog

This will show a list of recent actions that Git has performed against your repository:

c6ec292 HEAD@{0}: revert: Revert "Fix date format in DateRange"
524fb37 HEAD@{1}: checkout: moving from hahmed-search-users to master
6ab1f34 HEAD@{2}: pull https://github.com/hahmed/octokit.net.git search-users: Merge made by the 'recursive' strategy.
96682a8 HEAD@{3}: checkout: moving from bring-back-microsoft-bcl-async to hahmed-search-users
51f13a1 HEAD@{4}: reset: moving to origin/bring-back-microsoft-bcl-async
4565789 HEAD@{5}: pull https://github.com/hahmed/octokit.net.git search-users: Merge made by the 'recursive' strategy.
51f13a1 HEAD@{6}: commit: corrected package folders to use Portable versions
16b19a7 HEAD@{7}: reset: moving to HEAD~1
91c722e HEAD@{8}: reset: moving to 91c722e
d6581fa HEAD@{9}: rebase -i (finish): returning to refs/heads/bring-back-microsoft-bcl-async
d6581fa HEAD@{10}: rebase -i (pick): eureka, this now deserializes to a dictionary correctly :metal:
083a9b3 HEAD@{11}: rebase -i (pick): switched to use the CommentPulldown built on c17c4e8 which works around an annoying
...

You can see the first line of the reflog is the revert I did. Yours might be further down the list.

What's important here is the row before that revert - this is the point in time we want to go back to.

So this line

524fb37 HEAD@{1}: checkout: moving from hahmed-search-users to master

is the one we want to reset our branch to. Note the identifier as the first value, 524fb37 - that's the abbreviated commit SHA we'll use in the next step. Yours is likely to be different.

So back in the shell we'll use that commit SHA to reset the current branch to a point in time.

git reset [SHA] --hard

Aside: when you reset a branch to another point in time, Git will take the changes in the working tree between now and then and roll them up into a collection of modified files. The --hard here is a flag to tell Git to throw away those changes. You can also do --soft to immediately stage these changes, ready to commit - or the default which is to keep the changes around as unstaged.

So after doing a reset, run git reflog again and you'll see a new entry:

524fb37 HEAD@{0}: reset: moving to 524fb37
c6ec292 HEAD@{1}: revert: Revert "Fix date format in DateRange"
524fb37 HEAD@{2}: checkout: moving from hahmed-search-users to master
6ab1f34 HEAD@{3}: pull https://github.com/hahmed/octokit.net.git search-users: Merge made by the 'recursive' strategy.
96682a8 HEAD@{4}: checkout: moving from bring-back-microsoft-bcl-async to hahmed-search-users
51f13a1 HEAD@{5}: reset: moving to origin/bring-back-microsoft-bcl-async
4565789 HEAD@{6}: pull https://github.com/hahmed/octokit.net.git search-users: Merge made by the 'recursive' strategy.
51f13a1 HEAD@{7}: commit: corrected package folders to use Portable versions
16b19a7 HEAD@{8}: reset: moving to HEAD~1
91c722e HEAD@{9}: reset: moving to 91c722e

And everything should be sunshine and rainbows again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment