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 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.
// It gives the details of the previous commits to the branch$ 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 COMMIT_ID
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 --hard COMMIT_ID
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.
$ git reset — hard HEAD~1
- 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.
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.
I write the articles on tech stuff to share my knowledge, tips & tricks with all. You can visit here SidTechTalks.