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
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

Incredible Demos

  • By
    Reverse Element Order with CSS Flexbox

    CSS is becoming more and more powerful these days, almost to the point where the order of HTML elements output to the page no longer matters from a display standpoint -- CSS lets you do so much that almost any layout, large or small, is possible.  Semantics...

  • By
    Retrieve Your Gmail Emails Using PHP and IMAP

    Grabbing emails from your Gmail account using PHP is probably easier than you think. Armed with PHP and its IMAP extension, you can retrieve emails from your Gmail account in no time! Just for fun, I'll be using the MooTools Fx.Accordion plugin...

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!