Git undo a commit

Are you new to the Git world? Or earlier working on TFS to maintain the code base now moved to git? If you are working on some code piece and something not going as expected and now you want to roll back or undo a commit. In this article, I show you how to use git to undo a commit.

 

Git undo a commit

 

There are multiple commands you can use to undo a git commit below are the details-

1. Git undo a Commit using Git Revert Command

A revert command will undo/remove all the changes you made in a single commit to your source code repository.

Suppose if a git commit adds a file named index.ts to the git repo then the revert command on that commit will remove the index.ts file from the repo. If we add any code piece to any file same will be removed after git revert from the repo.

 

Steps to Git undo a commit using git revert with an example –

 

 a) First Identify the commit

Then run the git log command to identify the commit that needs to be reverted.

$git log

The output of the git log will look like the below. Find the file2.txt commit in the output list–

git identify a commit

Commit <commit id>

Author: <author email>

Date:   Tue Jun 7 14:26:34 2021 +0530

<commit comment>

 

b) Execute git Revert to undo commit

 git revert <commit to revert>

 

Example –

Suppose you commit 3 files recently let’s say –

File1.ts

File2.ts

File3.ts

And in File2.ts you have to revert the changes that you commit in the previous commit.

Now run the git revert to undo a commit. Only copy the alphanumeric id and use the same in the revert command like below-

Git revert 8fb2473ac659d919cea31044e6f8ac9108a4bd06

Kindly note when a revert operation is performed, the changes from the targeted commit are removed from your local workspace, and also A new commit is created to reflect the new state of your repo.

 

2. Git Reset Command

 

With the reset command, you can undo the last commit but use it carefully because this command changes the commit history. it allows you to RESET your current head to a specified state. You can reset the state of a particular file as well as an entire branch.

git reset –hard HEAD~1

 

You can use the –hard option if you want to reset to the last commit and also remove all changes you made locally.

 

git reset –soft HEAD~1

 

Unlike the hard, the –soft option means that you will not lose the uncommitted changes if you have any.

 

Git undo a committed example using Git Reset –

Suppose you did a bad commit in your local branch now I want to remove that commit. Run the git log command to identify the commit-

$ git log

#Latest commit. This would refer as 'HEAD'.
commit <commit Id> : bad commit   

#Second to last commit. This is the one we want to restore.
commit <commit Id> : good commit

 

To restore everything back to the second last good commit, we need to reset the commit before the HEAD state:

#Use --soft option if you want to keep your local changes
git reset --soft HEAD^  
   
#Use --hard option if you don't care about keeping the changes if any.
git reset --hard HEAD^

 

Which Option is best to undo a commit?

As I said earlier git revert doesn’t overwrite the commit history so if they commit to reverting has already been pushed to the shared repo use git revert.

The git reset overwrites the commit history so if the commit being reset only exists locally use git reset.

 

Conclusion

I hope now you understand how to use git revert and git reset to undo a git commit. Using these two options you can undo a bad commit.

Posted in git