How to revert Git commit

Updated: January 27, 2024 By: Guest Contributor Post a comment

Git is an essential tool for developers all around the world. It’s a distributed version control system which tracks changes made to source code over time. Occasionally, developers find themselves needing to revert changes they’ve made to a repository. In this tutorial, we’ll explore several methods to revert a Git commit, ranging from basic to more advanced techniques. Understanding these commands will help you maintain a clean and organized commit history in your projects.

Understanding Git Revert

Before diving into the commands, it’s crucial to understand what reverting a commit in Git actually means. A revert operation will create a new commit that undoes the changes made by a previous commit. This is different from the “git reset” command, as it doesn’t alter the existing commit history.

Basic Revert Operation

To revert the changes made by the most recent commit, use the following command:

git revert HEAD

This creates a new commit that undoes the changes of the last commit. After running it, you’ll be prompted to edit a commit message for the reverting commit.

Reverting a Specific Commit

If you need to revert a specific commit, you should first find its commit ID using:

git log

With the commit ID, you can then run:

git revert <commit-id>

Replace ” with the actual commit hash. This will undo the changes from the specified commit without modifying history.

Advanced Reverting Techniques

What if you want to revert a string of commits, or deal with merge commits? The revert command can also handle these scenarios.

Reverting a Range of Commits

To revert a range of commits, use the following syntax:

git revert <old-commit-id>...<new-commit-id>

The above command will revert all commits from ‘old-commit-id’ up to and excluding ‘new-commit-id’.

Reverting a Merge Commit

Merge commits are a bit different because they involve two parent commits. You will need to specify which parent you want to keep with the ‘-m’ flag:

git revert -m 1 <merge-commit-id>

This will keep the first parent’s changes and revert the second parent’s changes. If you need to keep the second parent’s changes, replace ‘1’ with ‘2’.

Undoing a Revert

Sometimes, you may find that you’ve reverted the wrong commit. Fortunately, you can undo a revert with another revert:

git revert <revert-commit-id>

Complex Scenarios

In some cases, especially when dealing with older commits or those in branches, you might encounter conflicts. These are situations where automated tools such as Git cannot automatically decide how to incorporate changes. It’s a common scenario that requires manual intervention. Upon running the revert command, Git will notify you of any conflicts. It’s your responsibility to open the files, resolve the conflicts, and then complete the revert process by adding the files to the staging area and committing the changes, like so:

git add .
git commit -m "Resolved revert conflicts."

Important Considerations

When reverting commits that have been shared with others or are a part of a public repository, communication with your team is key. A revert potentially generates conflicts with other developer’s work, so it is best to discuss the decision before pushing the revert commit.

Conclusion

Reverting changes in Git allows you to undo alterations to your code without disrupting the project’s history. With the practices and commands suggested in this tutorial, you’ll be better equipped to manage your code changes and maintain a coherent commit history in your projects.

By grasping the essence of reverting commits in Git and applying the right command in each scenario, mishaps can become mere hiccups in the development process. With diligence and practice, these commands become second nature, allowing you to focus more on development and less on version control complexities.