Using git reflog to Fix Rebase Problems

By  on  

My git jedi skills aren't top class yet but I'm learning how to use the force.  I recently rebased  a pull request, only to find that it added about two dozen more commits, thus my branch was hosed.  After a bit of research, I found git reflog, a utility to view previous actions and get the information I needed to fix my rebase.

When you execute git reflog, you get a massive list of your previous actions.  The list will look like:

d2ed542 HEAD@{0}: checkout: moving from badges-ui-rebase to sign-in-width
fd69f6e HEAD@{1}: rebase -i (finish): returning to refs/heads/badges-ui-rebase
fd69f6e HEAD@{2}: rebase -i (squash): First pass at badge improvements
f79a769 HEAD@{3}: checkout: moving from badges-ui-rebase to f79a769
552473c HEAD@{4}: commit: First pass at badge improvements
f79a769 HEAD@{5}: rebase -i (finish): returning to refs/heads/badges-ui-rebase
f79a769 HEAD@{6}: commit: More
e9227ed HEAD@{7}: checkout: moving from badges-ui-rebase to e9227ed257ea1966fffc
5f0d862 HEAD@{8}: checkout: moving from master to badges-ui-rebase
e9227ed HEAD@{9}: pull mozilla master: Fast-forward
bf9084a HEAD@{10}: checkout: moving from sign-in-width to master
d2ed542 HEAD@{11}: rebase -i (finish): returning to refs/heads/sign-in-width
d2ed542 HEAD@{12}: checkout: moving from sign-in-width to d2ed542
d2ed542 HEAD@{13}: checkout: moving from use-strict to sign-in-width
4f3443d HEAD@{14}: rebase -i (finish): returning to refs/heads/use-strict
4f3443d HEAD@{15}: rebase -i (squash): Adding 'use strict' to JS files
294c376 HEAD@{16}: rebase -i (pick): Adding 'use strict' to JS files
bf9084a HEAD@{17}: checkout: moving from use-strict to bf9084ae49c269ce85651b829
824920e HEAD@{18}: commit: fix
47f9969 HEAD@{19}: checkout: moving from badges-ui-rebase to use-strict
5f0d862 HEAD@{20}: rebase -i (finish): returning to refs/heads/badges-ui-rebase
5f0d862 HEAD@{21}: rebase -i (squash): First pass at badge improvements
95ae782 HEAD@{22}: checkout: moving from badges-ui-rebase to 95ae782
a75a292 HEAD@{23}: commit: Fix else error
#....

When I found the commit I wanted to roll back to, I used git reset to do so:

# git reset --hard HEAD@{##}
git reset --hard HEAD@{87}

Awesome!  Instead of needing to start over with my changes, I was able to retry my rebase and get things fixed.  git reflog can be a massive time-saver!

Recent Features

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • By
    Upload Photos to Flickr with PHP

    I have a bit of an obsession with uploading photos to different services thanks to Instagram. Instagram's iPhone app allows me to take photos and quickly filter them; once photo tinkering is complete, I can upload the photo to Instagram, Twitter, Facebook, and...

  • By
    jQuery Chosen Plugin

    Without a doubt, my least favorite form element is the SELECT element.  The element is almost unstylable, looks different across platforms, has had inconsistent value access, and disaster that is the result of multiple=true is, well, a disaster.  Needless to say, whenever a developer goes...

Discussion

  1. I agree! I was so relieved when I discovered this, I thought my work was gone forever. :)

  2. It’d be more time saver and help you keep track of your work if you make a branch and reset hard to base ( Head 87 in your case ). Then commit it and merge the branch to current branch.

    This way you can again go to certain point as you please.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!