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:
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:
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:
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.
Also, my staging area is clean.
Now, to remove or undo the last commit, run the following command:
As you can see, the commit aec00f3 is gone.
Also, the file I modified in the last commit is in my staging area.
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.
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.
Now, to remove the last commit and remove the changes in that commit, run the following command:
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.
The staging area is also clean. So, changes files from the removed commit exist.
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:
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.