How To undo the most recent local commits in Git? Step By Step Guide #2023

If you’ve ever made a mistake while working on a project, you’ll be happy to know that Git makes it easy to undo your last few commits. In this article, we’ll show you how to undo the most recent local commits in Git. Keep in mind that this will erase all of the changes that were made in those commits. If you only want to revert a single commit, check out our guide on reverting individual commits.

First, let’s take a look at the Git history for our project. We can use the git log command to do this.

$ git log

commit e73164f75b60a01cb37794c65fa0fda86ae7411b

Author: John Doe

Date: Mon Jan 14 15:27:32 2019 -0500

Update README.md

 

commit dae86ef35baca01cb37794c65fa0fda86ae7411b

Author: John Doe

Date: Mon Jan 14 15:26:32 2019 -0500

 

As you can see, we have two commits. The most recent commit is at the top of the list. In order to undo this commit, we’ll use the git reset command.

$ git reset –soft HEAD~

This command will undo the most recent commit while keeping all of the changes that were made in that commit. If we run the git log command again, we can see that our most recent commit has been removed.

 

If you need to undo the most recent local commits in Git, there are a couple of different ways that you can do this. The first way is to use the “git reset” command. This will remove all of your staged changes, and it will also revert your head back to the last commit.

The second way is to use the “git revert” command. This will create a new commit that undoes all of the changes from your most recent commit. Which method you use will depend on what exactly you need to undo.

If you just need to remove some staged changes, then “git reset” is probably what you want. But if you want to actually undo a commit, then “git revert” is what you’ll need to use.