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
    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...

  • 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
    Styling CSS Print Page Breaks

    It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page. During the development of my...

  • By
    Face Detection with jQuery

    I've always been intrigued by recognition software because I cannot imagine the logic that goes into all of the algorithms. Whether it's voice, face, or other types of detection, people look and sound so different, pictures are shot differently, and from different angles, I...

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!