Git

How to Undo Last Commit in Git

A common workflow in Git is that you make changes to your project, add the changes to the staging area, commit the changes, make new changes, stage the changes, commit the changes, and it goes on and on. But what if you mistakenly committed your changes? Well, don’t fear. You can always undo your last commit in Git. In this article, I am going to show you how to undo the last commit in Git. So, let’s get started.

Setting Up a Test Git Repository:

In this section, I am going to clone one of my GitHub repository on my computer to set up a test repository for this article. If you want to follow along, then you may do so as well. If you have your own Git repository to work with, then great! You can use it as well.

To clone one of my GitHub repository (https://github.com/dev-shovon/my-project), run the following command:

$ git clone https://github.com/dev-shovon/my-project undo_commit_demo

The GitHub repository should be cloned into undo_commit_demo/ directory.

Now, navigate to the undo_commit_demo/ directory as follows:

The commit you want to undo may be in a different branch as mine. I want to undo the last commit of the ie-fix branch. So, I have to pull the branch ie-fix from my GitHub repository.

You can pull the ie-fix branch from GitHub as follows:

$ git pull origin ie-fix

NOTE: Here, ie-fix is the branch name.

The ie-fix branch should be pulled.

Now, checkout to the branch (in my case, ie-fix) from which you want to undo last commit as follows:

$ git checkout ie-fix

As you can see, the last commit of the ie-fix branch is aec00f3. In the next sections, I am going to show you ways of undoing the last commit on your Git repository.

Undo Last Commit and Keep Changes:

One way to undo the last commit is the use soft reset on your Git repository. What this does is, the last commit is removed and the changes you’ve made in that commit is added to the staging area of your Git repository. This way, if you want to fix anything, you can modify the files and add a new commit.

As you can see, the last commit in my ie-fix branch is aec00f3.

$ git log --oneline

Also, my staging area is clean.

$ git status

Now, to remove or undo the last commit, run the following command:

$ git reset --soft HEAD~1

As you can see, the commit aec00f3 is gone.

$ git log --oneline

Also, the file I modified in the last commit is in my staging area.

$ git status

Now, you can further modify the files, fix the mistakes you’ve made and commit the changes again.

Undo Last Commit and Remove Changes:

If the last commit which you want to remove is useless to you, then you may consider a hard reset on your Git repository. What hard reset does is, it removes the last commit as before. But, it also removes all the changes you’ve made in the last commit. Do a hard reset only when you’re sure that you no longer need anything of the last commit.

I am going to pull the ie-fix branch from my GitHub repository again to recover the last commit aec00f3 and remove it again in this section.

$ git pull origin ie-fix

As you can see, the last commit is back. If your Git repository is uploaded to a Git cloud service such as GitHub, then you can restore any commit you remove mistakenly as well.

As you can see, my staging area is clean.

$ git status

Now, to remove the last commit and remove the changes in that commit, run the following command:

$ git reset --hard HEAD~1

The last commit should be removed and the HEAD pointer should be updated.

As you can see, the commit aec00f3 is removed and the commit immediately before that (3fffdee) is the current the last commit.

$ git log --oneline

The staging area is also clean. So, changes files from the removed commit exist.

$ git status

Updating Remote Git Repository:

Now that you’ve successfully removed the faulty commit from your Git repository, you may want to update your GitHub repository as well. This is the topic of this section.

As you can see, git status also shows that I am 1 commit behind from the remote repository.

Before I updated my GitHub repository, the commit aec00f3 exists even though I removed it from the local Git repository as you can see.

To sync the local Git repository with the GitHub repository, run the following command:

$ git push --force origin ie-fix

The changes of the local Git repository should be synced with the GitHub repository.

The GitHub repository should be updated. As you can see, the commit aec00f3 is no longer listed. The commit immediately before aec00f3, which is 3fffdee is now the last commit.

So, that’s how you undo last commit in Git. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.