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
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    Jack Rugile’s Favorite CodePen Demos

    CodePen is an amazing source of inspiration for code and design. I am blown away every day by the demos users create. As you'll see below, I have an affinity toward things that move. It was difficult to narrow down my favorites, but here they are!

  • By
    Fading Links Using jQuery:  dwFadingLinks

    UPDATE: The jQuery website was down today which caused some issues with my example. I've made everything local and now the example works. Earlier this week, I posted a MooTools script that faded links to and from a color during the mouseover and mouseout events.

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!