How To Revert A Commit With Git Revert: Step-by-Step Guide

Last Updated : 04/28/2025 10:57:18

git revert is a powerful command that creates a new commit which undoes the changes made in a target commit. A commit in Git represents a snapshot of your repository at a given point in time.

How To Revert A Commit With Git Revert: Step-by-Step Guide

Let's break down how to use git revert to undo changes introduced by a specific commit in Git.

git revert is a powerful command that creates a new commit which undoes the changes made in a target commit. It's a safer approach than git reset for shared repositories because it doesn't rewrite history.

Git Commit


A commit in Git represents a snapshot of your repository at a given point in time. It includes changes made to files and a commit message describing the changes.

Syntax of git commit command
git commit -m "commit message"​

To know more about git commit, check out this article.

Git Revert


It might happen that we make some changes and then make a commit but later we discover a bug and we want to undo those changes and restore the project state to the previous commit so it does not create problems for others and people can work smoothly on the project. The git revert command helps us to do that.

Syntax of git revert command
git revert <commit-hash>​

Points To Remember


* When we do a git revert it does not delete the reverted commit from the commit history instead it stages the changes for another commit with the reverted changes.

* When you revert a commit c then all the commits made after c will also get reverted.



Here's a step-by-step guide:


1. Identify the Commit You Want to Revert:


First, you need to find the SHA-1 hash (the long string of characters) of the commit you want to undo. You can do this using the git log command:
git log --oneline​

This will display a concise history of your commits, with each commit on a single line showing its short hash and commit message.
a1b2c3d (HEAD -> main) Added a new feature
e5f6g7h Fixed a critical bug
i8j9k0l Initial commit​

Let's say you want to undo the changes introduced by the commit with the hash a1b2c3d ("Added a new feature").


2. Execute the git revert Command:


Once you have the commit hash, use the git revert command followed by the hash:
git revert a1b2c3d​

What Happens When You Run git revert:

* Git Analyzes the Changes: Git looks at the changes introduced by the target commit (a1b2c3d in our example).

* Git Creates a New Commit: Git automatically creates a new commit that contains the inverse of the changes from the target commit. This effectively undoes the changes.

* Git Opens an Editor (Usually): Git will typically open your configured text editor with a pre-filled commit message. This message will usually indicate that this commit reverts the specified commit. You can modify this message if you want to add more context.

* You Save and Close the Editor: Once you save and close the editor, Git will finalize the revert commit.  

Example Commit Message (that Git might generate):
Revert "Added a new feature"

This reverts commit a1b2c3d.​

3. The Result:


After the git revert command completes, your repository's history will have a new commit. This new commit will effectively remove the changes that were introduced by the commit you reverted. Your working directory and staging area will reflect these undone changes.

Important Considerations:
* Shared Repositories: git revert is the recommended way to undo changes in a shared repository. It makes the undo operation explicit in the commit history, making it clear to everyone what happened and avoiding the potential problems that can arise from rewriting history with git reset.  

* Multiple Commits: You can revert a range of commits. However, it's generally recommended to revert one commit at a time to avoid potential conflicts and make the history clearer.

* Conflicts: If the changes in the commit you're reverting conflict with changes made in subsequent commits, Git might prompt you to resolve these conflicts manually, just like during a merge.

* The Reverted Commit Still Exists: Importantly, git revert does not delete the original commit. It creates a new commit that undoes its effects. The original commit remains in the history.


How Does Git Revert Command Work?


let's dive deeper into how the git revert command actually works under the hood.

At its core, git revert operates by carefully calculating and applying the inverse diff of the target commit. Here's a breakdown of the process:

1. Identifying the Target Commit:

* When you provide a commit hash (e.g., git revert abcdef123), Git first identifies the specific commit you want to undo. It retrieves the snapshot of the repository's state before that commit was made and the snapshot after that commit was made.


2. Calculating the Diff:

* Git then calculates the diff (the set of changes) that were introduced by the target commit. This diff essentially describes what lines were added, deleted, or modified in the files between the "before" and "after" snapshots of that commit.


3. Generating the Inverse Diff:

This is the crucial step. git revert doesn't simply go back to the previous commit. Instead, it computes the inverse of the diff it just calculated.

* Additions become deletions: If the target commit added lines to a file, the inverse diff will specify the deletion of those same lines.

* Deletions become additions: If the target commit deleted lines, the inverse diff will specify the addition of those same lines (effectively restoring them).

* Modifications are reversed: If the target commit modified lines, the inverse diff will contain the changes needed to go back to the original content of those lines.


4. Applying the Inverse Diff:

* Git then takes the current state of your branch (typically the HEAD commit) and applies the inverse diff. This means it makes the changes in your working directory and staging area that effectively undo the changes introduced by the target commit.


5. Creating a New Commit:

Finally, Git automatically creates a new commit that records these changes (the applied inverse diff). This new commit becomes the latest commit on your current branch.

The metadata of this new commit will typically include:

* A unique SHA-1 hash.

* The author and committer information (usually the same as the user running the git revert command).

* A commit message (often pre-filled by Git indicating it's a revert of the specified commit).
 
* The parent commit(s) of this new revert commit will be the commit that was HEAD before you ran the git revert command. The original commit being reverted is also often referenced in the commit message.


Analogy:

Think of it like this: Imagine you have a recipe (your Git history). A commit is like a step in the recipe where you add or modify ingredients. git revert isn't like going back to a previous version of the recipe. Instead, it's like creating a new step that explicitly undoes the changes made in a specific earlier step. If a step said "add 1 cup of flour," the revert step would say "remove 1 cup of flour."

Why This Approach is Safe for Shared Repositories:

* Non-Destructive: git revert doesn't rewrite the existing history. The original commit remains untouched. Instead, it adds a new commit to the history that explicitly undoes the changes.

* Clear Intent: The revert operation is clearly recorded in the commit history, making it transparent to everyone collaborating on the project. Everyone can see why a certain set of changes was undone.

* Avoids History Rewriting Issues: Rewriting history (using commands like git reset --hard on commits that have already been pushed) can cause significant problems for other collaborators who have already based their work on that history. git revert avoids these issues by adding a new, forward-moving commit.  

In essence, git revert is a way to safely and explicitly undo changes in your Git history by creating a new commit that applies the opposite of the changes introduced by a specific commit. It's a fundamental tool for managing changes in collaborative Git workflows.


How to Revert a Commit in Git After Push?


The revert a commit in Git after Push creates a new commit that undoes the changes of the previous commit without altering the commit history. This is the best and safest way to undo a commit in a shared repository because it avoids rewriting history.

Here’s the step-by-step guide to revert a Commit after Push:

1. Check the Commit History
Use git log to identify the commit you want to undo:
git log​

Look for the hash of the commit you want to revert (example: 3e1f1b2).


2. Run the Revert Command
Use the commit hash with git revert:
git revert 3e1f1b2​

This will open the editor to confirm the default commit message. Save and close the editor to complete the revert.


3. Push the Revert Commit
After the revert is committed locally, push it to the remote repository:
git push origin main​

This adds a new commit that undoes the changes without rewriting history.

How to Revert Multiple Commits in Git?


Git allows you to revert multiple commits without rewriting history. This is especially useful when you’ve already pushed the changes or are working collaboratively.

Syntax:
git revert <oldest_commit_hash>^..<newest_commit_hash>​

* The caret (^) includes the oldest commit in the range

* Git will revert each commit individually, creating a new one for each

Example 2: Revert a Feature Rollout
The commit history looks like the following:
a1b2c3d - Added feature A (latest)

f6g7h8i - Updated tests

j1k2l3m - Initial setup for feature A​

To revert all three commits related to feature A, run:
git revert j1k2l3m^..a1b2c3d​

Git will create three new revert commits, one for each original commit.


What is the difference between git revert and git reset?



Git Revert

Git Reset

Revert changes by creating new commits.

Reset the current HEAD to a specified state.

Creates a new commit that undoes the specified commit.

Moves HEAD and branch pointers to the specified commit.

Preserves commit history by adding revert commits.

Rewrite commit history by removing commits.

Preferred for reverting changes on shared branches.

Should not be used for shared branches due to history rewriting.

Syntax: git revert [options] <commit-hash>

git reset [options] <commit>


Git Revert Command Summary


Here’s a table summarizing the various Git Revert Commands:

Action

Command

Description

View commit history

git log

Shows commit hashes and messages

Revert a single commit

git revert <commit_hash>

Creates a new commit that undoes the selected commit

Revert multiple commits (range)

git revert <oldest_hash>^..<newest_hash>

Reverts all commits in the specified range

Revert a merge commit

git revert -m 1 <merge_commit_hash>

Reverts a merge (use -m 1 to specify mainline)

Push changes after revert

git push origin <branch_name>

Pushes the new revert commit to remote repo

Resolve conflicts (if any)

git add . → git revert --continue

Handles conflicts during revert


Note : This article is only for students, for the purpose of enhancing their knowledge. This article is collected from several websites, the copyrights of this article also belong to those websites like : Newscientist, Techgig, simplilearn, scitechdaily, TechCrunch, TheVerge etc,.