How to revert a commit in Git: As a developer, you have to use Git every day for keeping the track your code.
You can easily install git in your system and start collaborating with other developers.
Sometimes, we commit the changes in the repository or local environment, which creates issues in the project. That’s why we often have to use the revert command to undo our changes or want to get back to the previous commit.
So, in this article, we will take a deep dive into the working of the git revert command and its usage.
Similar to the Git revert command, there is one more command, which is the Git reset command. Both of these commands have similar functionality with minor changes, which is very important for us to know.
Let’s first dive into how the git reset command works — then we’ll explore the magic behind git revert.
Table of Contents
GIT RESET
Let’s understand the working of Reset with an example.
First, create a text file and initialize the Git repository using the “git init” command.
Then, put the text file into the staging area using the “git add .” command. Followed by that, commit the changes using ‘git commit -m “your commit message” ‘.
This will create the commit with the commit ID, which you can see using the git log command.
Now, edit or add the text in the text file and again add the changes to the staging area using the ‘git add .’ command and commit your changes.
Now, you have 2 commits in the history, which you can check using the ‘git log’ command.
As you can see, we have 2 commits in the history. The first commit with commit id – e1058a531aebebcbc486a7798f84a4ab1704bdf9 and the second commit with commit id – d210d160bd0da1b33d6057c8e15a22a0aba8e28c.
So, if you want to undo the recent changes or go back to the changes you made till the first commit (e1058a531aebebcbc486a7798f84a4ab1704bdf9) and delete the second commit (d210d160bd0da1b33d6057c8e15a22a0aba8e28c) changes from the history as well, then you have to use the below commands.
1. GIT RESET MIXED
Using this command, you can delete the commit and its history. The changes of the deleted commit will not be deleted from the code or local repository, but will be in the unstaged area.
A reset mixed is a default command. So, you don’t need to provide “mixed” in your query.
git reset –mixed commitId git reset commitId
Both of the above commands work the same.
Now you will have the option to either edit or delete the changes of your commit.
As you can see in the above image, we have used the git status command, and the changes are removed from the commit and added to the unstaged area.
2. GIT RESET SOFT
Using the soft query in the reset command, you can delete the commits and their history, but unlike mixed commands, the changes of the deleted commit will show in the code in the staging area.
But first, let’s commit the changes that we unstaged using the reset mixed command above.
Now, check the commit ID and use the reset soft command to delete the commit from history.
So, here also you will have the option to edit and commit again or delete the changes from the staging area.
Now, let’s understand the most used form of the git reset command, which is the reset hard command.
3. GIT RESET HARD
Using this command, you can completely delete the commit and its history from the repository.
First, commit the changes again, which we had put in the staging area using the reset soft command.
Now, check the commit ID using the git log and use the reset hard command.
Unlike mixed and soft commands, the reset hard command doesn’t keep the code in an unstaged or staging area. It completely deletes the commit history and its code from the local machine as well.
Now, let’s understand the workings of the git revert command with example.
GIT REVERT
Using the git revert command, you can remove the changes made by a particular commit, but the commit history won’t be deleted.
So, it is a much safer way compared to the reset command. As in the reset command, the commit history also gets deleted.
When you use the revert command, the particular commit for which you want to revert doesn’t get deleted. However, after that commit (which you want to revert), a new commit is created with the changes removed from the previous commit (which you want to revert).
Now, let’s understand this with an example.
Previously, we were deleting the second commit using the git reset hard command. Let’s again make those changes and commit them to understand the working of the git revert command.
Using the git log command, we can see that a new commit was created with the changes we made recently. Now, use the git revert command to revert the changes and create a new commit with those changes.
You can notice that, unlike the reset command (where we were giving the commit ID just before the one we wanted to remove), here we are giving the commit ID of the commit that we want to revert.
After that, it will open Vim in case you want to change the default commit message.
Using the git log command, you can see that our old commit (81749c71bbb99707f98ee24211bb219b14e16547) is still in the commit history, and a new commit (fa58b00a609480c9e4370c63fb6062dde8823886) is created above it.
Using the revert command, we have to give the commit message using Vim, which can be annoying for some of us.
So, if you don’t want to change the default message of the commit and avoid using Vim, then you can make use of the –no-edit query with the revert command.
This will revert your changes with a new commit without asking for a commit message in Vim.
Hope you like the article on Git revert and reset command. If you have any suggestions or queries, let me know in the comment section below. Till then, keep learning.