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
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    prefers-color-scheme: CSS Media Query

    One device and app feature I've come to appreciate is the ability to change between light and dark modes. If you've ever done late night coding or reading, you know how amazing a dark theme can be for preventing eye strain and the headaches that result.

  • By
    Image Reflection with jQuery and MooTools

    One subtle detail that can make a big difference on any web design is the use of image reflections. Using them too often can become obnoxious but using reflections on large, "masthead" images is a classy enhancement. Unfortunately creating image reflections within your...

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!