Git Revert

In the realm of Git, we frequently create numerous commits within our repository. However, there are instances where we come to the realization that certain commits are unnecessary and do not contribute positively to our project. In such cases, we turn to the Git Revert command, which enables us to reverse these commits.

Unlike some other Git commands that alter the project's history, Git Revert operates by generating a new commit to reverse the changes made in previous commits. Let's delve into the process of using 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

Reverting Commits

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.

Viewing the commit history 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.

Running the Git Revert command with the --no-edit option to revert to the previous commit.

Now, we can see the new revert commit by using the Git Log command.

Viewing the new revert commit

Reverting Merges

The Git Revert command is a versatile tool that can be employed to undo branch mergers, particularly those that have already been pushed to the remote repository. This process results in the creation of a new commit that effectively reverses the changes made during the merge.

To carry out merge reversals using Git Revert, the -m flag is utilized. Since merge commits typically have multiple parents, it is essential to specify which parent branch's changes you want to undo. This can be determined by inspecting the merge commit's parents using the Git Log command. In most cases, the intention is to revert to the branch into which the merge was initially performed, typically denoted as parent 1.

$ 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.

Viewing the merge commit and its parents by using the Git Log command.

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.

Use the Git Revert with -m to revert the merge commit

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.

Viewing the new revert commit by using the Git Log command

Summary

The Git Revert command is a useful tool for reversing changes in a Git repository without affecting the commit history. Unlike Git Reset, which can rewrite the commit history, Git Revert creates a new commit that essentially undoes the changes made in the unwanted commits. This approach preserves the commit history while reflecting the reversal of changes. Git Revert can be applied to both regular commits and merge commits. When dealing with a merge commit, it's important to specify the parent to which you intend to revert the changes.

No comments:

Post a Comment