In this post, we’ll cover how to uncommit in Git. If you mistakenly executed a git commit but haven’t pushed it to the remote repository yet, you can undo the last commit without losing your changes or data.

Git Uncommit: How to Undo the Last Commit

Although there’s no specific git uncommit or git undo command, Git provides other commands like git reset, git clean, and git revert to manage workflows.

The need for uncommitting in Git is genuine, such as when you incorrectly commit files using conventional or semantic commit that should have been committed in a different context.

Git Undo Last Commit: 4 Ways to Uncommit

Undoing a commit can be risky if you’re not familiar with the process, but it’s straightforward if you understand how it works. Here are four different ways to undo a git commit.

Let’s assume the following context, where C is your HEAD, and (F) represents the state of your files:

   (F)
A-B-C
  master

git reset –hard

Use git reset --hard to undo the commit and completely remove all changes. In our scenario, commit C would be destroyed, and any uncommitted changes would be discarded.

Run:

git reset --hard HEAD~

The result:

 (F)
A-B
master

So using git reset --hard HEAD~ now B is the HEAD. Because of the use of --hard, the files are reset to their state at commit B.

Recover git reset –hard

If you did git reset --hard but need to get that destroyed code back there’s still a way to get it. Run this command:

git reflog

The git reflog will show you a list of (partial) commit shas (that is, hashes) that you’ve moved around in. Then you can find the commit you destroyed, and run:

git checkout -b someNewBranchName shaYouDestroyed

You’ve now recovered that commit.

This can be done because commits don’t actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn’t mean to get rid of.

git reset

With git reset HEAD~, you can undo the commit and unstage all files.

This option is suitable if commit C wasn’t entirely wrong but required some adjustments. You can undo the commit, keep your changes, edit them, and then make a better commit.

Starting again with C as the HEAD commit:

   (F)
A-B-C
  master

To undo the commit and unstage files, just leave off the --hard option:

git reset HEAD~1

Now the the result would be:

   (F)
A-B-C
master

When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use –hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven’t lost any changes!

git reset –soft

Using git reset --soft HEAD~ you can undo commit and keep all files staged.

This is the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1

This not only leaves your files alone, it even leaves your index alone. When you do git status, you’ll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you’d be redoing the same commit you just had.

Cautions

  1. Beware that this actions might not do what you expect if your erroneous commit was a (fast-forward) merge! If your head is on a merge commit (ex: merged branch feature into master), git reset --hard~1 will point the master branch to the last commit inside the feature branch. In this case the specific commit ID should be used instead of the relative command.

  2. If the commit was previously pushed to the remote repository, any undo operation will cause some problems to the rest of the users who have this commit in their local copy, when they do a git pull in the future. So, if the commit was already pushed, run this instead:

git revert <bad-commit-sha1-id> 
git push origin

Conclusions

Even without a git undo command, what would be really trick to implement and use, Git offer options to back commit changes with the opportunity to redo mistakes and organize correctly your source code.