Git Undo Last Commit

By  on  

I'm a massive fan of git; it's super powerful and easy to use, especially when it comes to branching.  The biggest sin I commit when using git is adding files and then committing them...to master branch instead of a feature branch.  Oops.  Certainly don't want that.

If you've done a git add (files) and then commit them to the wrong branch, backing that out is easy:

git reset --soft HEAD~1

With the command above, the files are still added but not committed, so you can create your feature branch, do another git commit -m (message), and be on your way!

Recent Features

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    The Simple Intro to SVG Animation

    This article serves as a first step toward mastering SVG element animation. Included within are links to key resources for diving deeper, so bookmark this page and refer back to it throughout your journey toward SVG mastery. An SVG element is a special type of DOM element...

  • By
    Facebook-Style Modal Box Using MooTools

    In my oh-so-humble opinion, Facebook's Modal box is the best modal box around. It's lightweight, subtle, and very stylish. I've taken Facebook's imagery and CSS and combined it with MooTools' awesome functionality to duplicate the effect. The Imagery Facebook uses a funky sprite for their modal...

Discussion

  1. Yuriy Husnay

    The other way to achieve this, is

    git reset --soft HEAD^
    

    as HEAD^ is pointer to HEAD~1

    Personally, I have an alias git undo which is:

    git config --global alias.undo 'reset --soft HEAD^'
    
  2. MaxArt

    Git “easy to use”… Uh, what?
    It’s a very complete and powerful tool, no doubt about it, but I wouldn’t call it “easy”. There’s a plethora of options and unclear docs, that it takes a lot of time just to know they exist, not to mention actually use them and get used to them.
    For example, I knew about this trick, but didn’t know about Yuriy’s suggestion.

    That’s why I end up using a tool like SourceTree instead.

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