When working with Git, we will make a lot of commits in our repository. Later we may realize that some of these commits are unwanted and do not add any value to our project. The Git Revert command is used to undo commits in our repository.
Instead of rewriting the history of our project like some other commands, Git Revert will create a new commit to undo the changes. Let's learn how to use this command to undo commits.
How Git Revert Works?
- As discussed above, the Git Revert command is used to undo commits. One may wonder that the Git Reset command does the same thing and why use Git Revert? The difference is that Git Revert will undo a commit in a forward-moving fashion.
- Instead of rewriting the history of our project, it will simply create a new commit that has the inverse of all the changes of the commit that we want to undo.
- This way our project history is unmodified and, we have also removed the effects of the unwanted commit. This approach can be very helpful when working on shared branches or when pushing to a remote repository. If we alter the commit history of a branch and try to push it to a remote repository, then Git will block that push. We can still forcefully push the changes but this will lead to inconsistencies for other developers.
- Another advantage of Git Revert over Git Reset is that we can directly pick any commit from the history and undo it. But with Git Reset we will first have to continuously undo the commits up to that one unwanted commit and then reapply the changes of all the other commits. Essentially, Git Reset can only undo in a straight-line path and cannot jump commits in between.
Git Revert Command
The Git Revert command is fairly easy to use. We just need to mention the hash of the commit that we want to undo and the rest is handled by Git Revert.
$ git revert <commit-hash>
By default, the above command will open our configured text editor where we can alter the message of the new revert commit that will be created. We can skip this step and let Git autogenerate a message for our commit by using the --no-edit command. The autogenerated message is of the format - Revert "Commit Message of the Unwanted Commit".
$ git revert <commit-hash> --no-edit
Creating a new revert commit is the default behavior of the Git Revert command. We can use the --no-commit option to just add the inverted changes to the working directory and the staging area. This will not create a new commit. We can view the inverted changes and also alter them before running the Git Commit command to finally undo the commit.
$ git revert <commit-hash> --no-commit
Consider the following example to better understand how to use Git Revert.
Consider that we want to undo the second last commit of our feature branch. We can find its hash by using the Git Log command.
Next, we will run the Git Revert command and pass this commit hash. We will also use the --no-edit option as we want to keep the default commit message.
Now, we can see the new revert commit by using the Git Log command.
The Git Revert command can also be used to revert the merging of two branches. It is mostly used for reverting merges that have been pushed to the remote repository. This will also create a new commit that will have the inverted changes.
We use the -m flag with the Git Revert command to undo merges. We also need to specify which parent we need to revert changes to. This is needed because a merge commit has two or more parents. We can view these parents by using the Git Log command. Most of the time, we will need to revert to the branch we had merged into. We use the 1 to indicate this.
$ git revert -m 1 <hash of merge commit>
Consider the following example to better understand how to use Git Revert to undo a merge.
Suppose we had just merged a feature branch into our master branch. We can view the merge commit by running the Git Log command. The line that begins with Merge: denotes the parents of this merge commit. The first hash used to belong to the master branch (the branch we merged into) we had merged into and the second hash is of the feature branch.
Next, we will run the Git Revert command with the -m flag to revert this merge commit. We will also mention "1" to denote that we need to keep the changes(or the commit history) of the master branch. We will not alter the commit message.
Now, when we run the Git Log command, we can see that a new revert commit was added. We can also see that the commit message shows the hash of the commit to which changes were reverted.
Git Revert is a command that is used to undo changes in our repository. Unlike Git Reset, this command will not alter the commit history and creates a new revert commit that has all the inverted changes of the unwanted commits. We can use it to revert commits as well as merges. When reverting a merge commit, we must mention the parent to which we want to revert.