How To Undo The Most Recent Local Commits In Git

So you've committed something you didn't mean to and you're stuck. Let's take a look at how to undo the mistake.

Let's start with the mistaken commit:

$ git commit -m "Oh noes this is a huge mistake!"

Ok, so now what we're going to do is undo the commit, but leave the changes you had made "unstaged" so that you can still see them in git status as local changes.

$ git reset HEAD~

That's basically it.

Now you can make whatever changes you need, and then you can add the files that you want to commit back again:

$ git add ...FILES...

Finally, if you want to re-use the message you used last time, commit like this:

$ git commit -c ORIG_HEAD

If not you can just commit as usual:

$ git commit

Some alternatives

If you just want to edit the commit message of your last commit:

$ git commit --amend

If you want to undo the commit but keep the previous set of files staged ready to be comitted again:

$ git reset --soft HEAD~

If you want to completely blow away all changes from the last commit and get your working tree completely clean as if you'd never changed anything since the last commit, then do this (WARNING: this will destroy un-commited changes!):

$ git reset --hard HEAD~

There we go. That's how you undo the most recent local commit in Git. Hope this was helpful!

Hosted Gitea is a fully managed Gitea hosting service. If you're looking for a private alternative to GitHub or Gitlab check out our service.

Get your Gitea server