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
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

Incredible Demos

  • By
    Create Spinning, Fading Icons with CSS3 and MooTools

    A goal of my latest blog redesign was to practice what I preached a bit more;  add a bit more subtle flair.  One of the ways I accomplished that was by using CSS3 animations to change the display of my profile icons (RSS, GitHub, etc.)  I...

  • By
    Flashy FAQs Using MooTools Sliders

    I often qualify a great website by one that pay attention to detail and makes all of the "little things" seem as though much time was spent on them. Let's face it -- FAQs are as boring as they come. That is, until you...

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!