Hey everyone, many developers have faced the problem when they want to roll back the request (due to something broken in the code or any other reason) from the main branch. They were not aware of how to revert or undo the last commit in Git that they had done earlier.

You can undo or revert almost anything in Git. So, in this article, I will show you how to undo a merge in Git and understand the concept of rollback any previous or last commit changes. If you want to learn the basics of Git, visit here Important Basic Git Commands – Developers should know.

Let’s understand the concept with the help of an example.

Revert last commit

βœ… Get the last Commit ID

There are several ways to undo the last commit. You can use Git Revert to revert the last commit.

The git revert command is used for undoing changes to a repository’s commit history. It does not move ref pointers to this commit.

I will show you how to do it, with the help of an example.

Let’s say you have accidentally merged the code that you are not ready to. And you want to go back to the previous stage.

Git log

You will get the details like the above image. In these detail, there is a particular COMMIT_ID that defines the commit that we had done earlier.

βœ… Revert last commit

Now, when you get the hash of the commit (COMMIT_ID) you want to get back to, run –

git revert
Git Revert

This command will revert the last commit from the local repository. You need to run git push to change the same thing on the remote repository as well.

FinallyπŸŽ‰, you have reverted the last commit from the main branch.

βœ… Undo last commit

The other way of doing undo is using Git Reset.

Git Reset move the HEAD and branch ref pointers to a specified commit.

Let’s understand with an example.

git reset
Git Reset

You should see some things get removed from your code editor when you run the command.

If you are not sure of the last commit hash ID, then you can run this below command to do the same thing and go back to the commit before the merge.

πŸ“” Note:

  • When you use the --hard flag to undo a merge, any uncommitted change will be reverted. It means you lost your local uncommitted changes.
  • If you use --soft flag to undo a merge, it keeps your uncommited changes in the local directory.


If we compare the two methods then it seems that git revert is safe, and git reset is dangerous. As we’ve seen in our example, there is a possibility of losing work with git reset. With git revert, we can safely undo a public commit, whereas in the case of git reset, the --hard flag removes uncommitted changes, while the --soft flag keeps uncommitted changes.

That’s all about the undo or revert the last commit in git. I hope you learned how to undo or revert a merge in Git, in this way you can roll back a mistaken or unwanted merge and work more efficiently with Git.

I hope you enjoyed the article and if you found this useful, then please share it with your friends and colleagues. If this post helps you, then spread this so that other people can also benefit.

If you have any queries please feel free to post them in the comments section or anything that you want to ask through mail contact.

Thank youπŸ˜‰. Happy Learning πŸ™Œ

Also read,