Sling Academy
Home/DevOps/How to revert Git commit

How to revert Git commit

Last updated: January 27, 2024

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.

Next Article: GitHub and Remote Repositories: A Beginner’s Guide

Previous Article: How to re-attach HEAD to a previous Git commit

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide