Git Revert Command Explained (9 Practical Examples)

Git Revert Command Explained (9 Practical Examples)

The git revert command allows you to safely undo changes introduced by previous commits without rewriting the commit history. Instead of deleting commits, Git creates a new commit that reverses the earlier changes, making it safe for collaborative repositories.


Git Revert Command - Quick Cheat Sheet

The following quick reference table covers the most common git revert commands and scenarios you may encounter during daily Git workflows.

Command(s)Description
git revert <commit-hash>Reverts a specific commit by creating a new commit that undoes the changes introduced in that commit.
git revert HEADReverts the latest commit in the current branch.
git revert HEAD~1Reverts the second most recent commit.
git revert HEAD~NReverts a commit N steps before the current HEAD.
git revert <commit1> <commit2>Reverts multiple specific commits in sequence.
git revert <old-commit>..<new-commit>Reverts a range of commits between two commit hashes.
git revert HEAD~3..HEADReverts the last three commits in the repository history.
git revert -m 1 <merge-commit>Reverts a merge commit while keeping the first parent branch.
git revert --no-edit <commit>Reverts a commit without opening the commit message editor.
git revert --no-commit <commit>Applies the revert changes but does not immediately create a new commit.
git revert -n <commit>Applies revert changes to the working directory so multiple reverts can be committed together.
git revert --abortCancels the current revert operation if conflicts occur.
git revert --continueContinues the revert process after resolving conflicts.
git revert --quitStops the revert sequence without creating a commit.
git revert <commit> --strategy-option oursReverts a commit while preferring the current branch during conflicts.
git revert <commit> --strategy-option theirsReverts a commit while preferring incoming changes during conflicts.
git revert <commit> --signoffAdds a Signed-off-by line to the revert commit message.
git revert <commit> -SCreates a GPG-signed revert commit.
git revert <revert-commit>Reverts a previous revert commit to restore the original changes.

Git Revert Command Explained

What git revert does internally

The git revert command undoes the changes introduced by a specific commit by creating a new commit that applies the opposite changes. Instead of removing the original commit from the history, Git analyzes the selected commit and generates a reverse patch that cancels the modifications.

For example, if a commit added or modified files, the revert commit will remove or restore those files to their previous state. This new commit is then added on top of the current branch.

Because Git creates a new commit rather than modifying existing history, the original commit remains visible in the repository.

How git revert differs from deleting commits

A common misconception is that git revert deletes commits. In reality, it does not remove commits from the repository history.

Instead, Git performs the following actions:

  1. Identifies the commit to revert.
  2. Calculates the changes introduced by that commit.
  3. Creates a new commit that reverses those changes.

Example commit history before revert:

text
A → B → C

If commit C introduced a bug, running:

text
git revert C

creates a new commit D that reverses the changes from commit C:

text
A → B → C → D

Commit C still exists, but commit D cancels its effect.

Why git revert preserves commit history

git revert preserves history because it adds new commits instead of modifying existing ones. This behavior is important in collaborative environments where multiple developers work on the same branch.

Benefits of preserving commit history include:

  • Avoiding conflicts with other developers' work
  • Maintaining a complete audit trail of changes
  • Making it easier to understand when and why a change was reverted

Because of this, git revert is the recommended method for undoing commits that have already been pushed to a shared repository.


Git Revert Syntax and Options

Basic git revert syntax

The general syntax of the git revert command is:

text
git revert [options] <commit>

Example reverting a specific commit:

text
git revert a1b2c3

This command creates a new commit that reverses the changes introduced in commit a1b2c3.

You can also revert the most recent commit using:

text
git revert HEAD

This creates a new commit that cancels the latest commit in the current branch.

Important git revert flags explained

The git revert command supports several useful options that control how the revert operation behaves.

OptionDescription
--no-editReverts the commit without opening the commit message editor.
--no-commitApplies revert changes but does not automatically create a commit.
-nSame as --no-commit, allowing multiple revert changes before committing.
-m <parent>Used when reverting merge commits to specify which parent branch to keep.
--signoffAdds a Signed-off-by line to the commit message.
-SCreates a GPG-signed revert commit.
--abortCancels the revert operation if conflicts occur.
--continueContinues the revert process after conflicts are resolved.

Example reverting a commit without opening the editor:

text
git revert --no-edit <commit-hash>

Understanding the commit hash in git revert

Every commit in Git has a unique identifier called a commit hash. This hash is used by Git to identify the exact commit you want to revert.

You can find commit hashes using:

text
git log --oneline

Example output:

text
c4f8a7d Fix login bug
a8c21f1 Update README
e12a9f3 Initial commit

To revert commit c4f8a7d, run:

text
git revert c4f8a7d

Git will generate a new commit that undoes the changes introduced in that commit.


Git Revert Workflow Explained

How git revert creates a new commit

When you run git revert, Git does not remove the original commit. Instead, it creates a new commit that contains the inverse changes of the selected commit.

For example, if the original commit added a file, the revert commit will remove that file.

Example history before revert:

text
A → B → C

Running:

text
git revert C

creates:

text
A → B → C → D

Commit D contains the reverse changes of commit C.

What happens to HEAD after revert

After running a revert operation, Git creates a new commit and moves the HEAD pointer to that new commit.

Example:

Before revert:

text
A → B → C (HEAD)

After running:

text
git revert C

History becomes:

text
A → B → C → D (HEAD)

The HEAD now points to the new revert commit D.

This behavior ensures that the branch history remains intact while the unwanted changes are safely undone.

Visual explanation of git revert history

The revert workflow can be visualized as follows.

Original history:

text
A → B → C

Commit C introduces an issue.

Running:

text
git revert C

creates a new commit:

text
A → B → C → D

Commit D cancels the changes made in commit C, restoring the repository to the state it had after commit B, while still keeping the full history intact.


Revert the Latest Commit

Sometimes the most recent commit introduces a bug or incorrect change. In such cases, you can revert the latest commit safely using git revert. This creates a new commit that reverses the changes while preserving the repository history.

Revert the most recent commit using HEAD

To revert the latest commit in the current branch, run:

text
git revert HEAD

Git will open the default commit message editor with a message similar to:

text
Revert "previous commit message"

Once saved, Git creates a new commit that reverses the changes introduced by the latest commit.

Example commit history before revert:

text
A → B → C (HEAD)

After running git revert HEAD:

text
A → B → C → D (HEAD)

Commit D reverses the changes introduced by commit C.

Verify revert using git log

You can verify the revert operation by checking the commit history:

text
git log --oneline

Example output:

text
d3a4c2e Revert "Add login validation"
c2f91ab Add login validation
a1b3f11 Initial commit

The new revert commit appears at the top of the history.

Understanding the generated revert commit

The revert commit contains the inverse changes of the original commit. If the original commit added a file or modified code, the revert commit will undo those changes.

This approach ensures that:

  • the original commit remains in history
  • the repository state is restored
  • collaboration remains safe for shared branches

Revert a Specific Commit

Sometimes you need to revert a particular commit rather than the latest one. Git allows you to revert any commit using its commit hash.

Find commit hash using git log

To identify the commit you want to revert, run:

text
git log --oneline

Example output:

text
c4f8a7d Fix configuration bug
a8c21f1 Add new API endpoint
e12a9f3 Initial commit

Each entry contains a commit hash that uniquely identifies the commit.

Revert a commit using commit ID

To revert a specific commit, run:

text
git revert c4f8a7d

Git will generate a new commit that reverses the changes introduced in that commit.

Example history before revert:

text
A → B → C → D

Reverting commit B produces:

text
A → B → C → D → E

Commit E reverses the changes introduced in commit B.

Verify repository state after revert

After reverting, you can verify the changes using:

text
git log --oneline

You may also inspect file changes using:

text
git status
git diff

This confirms that the repository has returned to the expected state.

Revert Multiple Commits

In some cases, multiple commits may introduce incorrect changes. Git allows reverting several commits in one command.

Revert several commits using one command

To revert multiple commits at once, specify the commit hashes:

text
git revert <commit1> <commit2> <commit3>

Example:

text
git revert a1b2c3 d4e5f6 g7h8i9

Git will apply the revert operation sequentially for each commit.

How git creates multiple revert commits

Unlike some commands that combine changes, git revert creates a separate revert commit for each reverted commit.

Example history before revert:

text
A → B → C → D

If commits C and D are reverted, the history becomes:

text
A → B → C → D → E → F

Where:

  • E reverts commit D
  • F reverts commit C

Verify multiple reverts in commit history

You can confirm the revert operations using:

text
git log --oneline

Example output:

text
f7e21c3 Revert "Add feature X"
e3a91ab Revert "Update config file"
d4e5f6 Update config file
c3d2a1b Add feature X

Each reverted commit appears with a corresponding revert commit.


Revert a Range of Commits

Git also allows reverting multiple commits within a specific range.

Revert commits between two commit IDs

To revert commits between two commit hashes, use:

text
git revert <older-commit>..<newer-commit>

Example:

text
git revert a1b2c3..d4e5f6

This command reverts all commits that occurred between the specified commits.

Revert commits using HEAD ranges

You can also use relative references such as HEAD to revert recent commits.

Example reverting the last three commits:

text
git revert HEAD~3..HEAD

This instructs Git to revert commits starting three commits before the current HEAD.

Example reverting last three commits

Example commit history before revert:

text
A → B → C → D → E (HEAD)

Running:

text
git revert HEAD~3..HEAD

creates:

text
A → B → C → D → E → F → G → H

Where:

  • F reverts commit E
  • G reverts commit D
  • H reverts commit C

Each revert commit reverses the changes introduced in the original commits while preserving the complete commit history.


Revert a Merge Commit

Merge commits behave differently from normal commits because they contain changes from multiple parent branches. When reverting a merge commit, Git needs to know which parent branch should be considered the mainline.

Why reverting merge commits is different

A normal commit has one parent, but a merge commit has two parent commits. When you revert a merge commit, Git must determine which branch's changes should remain.

Example history:

text
A → B → C
        D → E

If commit E merges a feature branch into main, reverting it requires specifying the parent branch that should remain.

Without specifying the parent, Git cannot determine which changes to keep.

Using git revert -m option

To revert a merge commit, use the -m option:

text
git revert -m 1 <merge-commit-hash>

Example:

text
git revert -m 1 a1b2c3

The -m flag tells Git which parent branch should be treated as the mainline when generating the revert commit.

Choosing the correct parent branch

Merge commits usually have two parents:

ParentMeaning
-m 1Keep the first parent (usually the main branch)
-m 2Keep the merged branch changes

In most cases, you will use:

text
git revert -m 1 <merge-commit>

This keeps the main branch history while undoing the merged changes.


Revert a Commit That Was Already Pushed

When a commit has already been pushed to a remote repository, rewriting history using git reset can cause problems for other developers. In such cases, git revert is the safest option.

Why git revert is safe for shared repositories

The git revert command does not modify existing history. Instead, it creates a new commit that reverses previous changes.

Example history before revert:

text
A → B → C

Running:

text
git revert C

Creates:

text
A → B → C → D

Commit D cancels the changes introduced in commit C, while the history remains intact.

Because of this behavior, git revert is considered safe for shared branches and collaborative projects.

Reverting pushed commits without rewriting history

To revert a pushed commit, run:

text
git revert <commit-hash>

Example:

text
git revert HEAD

This creates a new commit that reverses the most recent change.

Unlike git reset, this approach does not require force pushing.

Push revert commit to remote repository

After reverting the commit locally, push the new revert commit to the remote repository:

text
git push origin <branch-name>

Example:

text
git push origin main

Other developers will then receive the revert commit when pulling changes.


Revert a Revert Commit

Sometimes a commit is reverted accidentally. In such cases, you can revert the revert commit to restore the original changes.

What happens when you revert a revert

When you revert a revert commit, Git applies the reverse of the revert changes. This effectively restores the original commit.

Example history:

text
A → B → C → D

Where:

  • C = original change
  • D = revert commit

Reverting commit D restores commit C.

How to restore a reverted change

First identify the revert commit using:

text
git log --oneline

Then revert that commit:

text
git revert <revert-commit-hash>

This creates a new commit that re-applies the original changes.

Practical example of revert of revert

Example history:

text
A → B → C → D

Where commit D reverted C.

Running:

text
git revert D

Creates:

text
A → B → C → D → E

Commit E restores the changes originally introduced in commit C.


Revert Changes Without Creating a Commit

In some situations you may want to review or combine revert changes before committing them. Git allows this using the --no-commit option.

Using git revert --no-commit

To apply revert changes without automatically creating a commit, run:

text
git revert --no-commit <commit-hash>

Example:

text
git revert --no-commit a1b2c3

This applies the reversed changes to the working directory but does not create a commit yet.

Modify revert changes before committing

Once the revert changes are applied, you can review or modify them.

Check the working directory:

text
git status

View the changes:

text
git diff

You can edit files before committing the revert.

Combine multiple reverts into one commit

You can apply multiple revert operations using --no-commit and then create a single commit afterward.

Example:

text
git revert --no-commit commit1
git revert --no-commit commit2
git revert --no-commit commit3

Then commit all changes together:

text
git commit -m "Revert multiple commits"

This approach keeps the commit history cleaner.


Revert Only Specific Files from a Commit

Sometimes you may want to revert changes affecting only certain files rather than the entire commit.

Revert changes affecting a single file

To revert a specific file from a previous commit, restore the file version from that commit.

text
git checkout <commit-hash> -- <file>

Example:

text
git checkout a1b2c3 -- config.yaml

This restores the version of the file from the selected commit.

Stage only selected revert changes

After restoring the file, stage the changes:

text
git add <file>

Example:

text
git add config.yaml

This allows you to revert only specific files instead of the entire commit.

Commit partial revert safely

Finally, commit the restored changes:

text
git commit -m "Revert changes to config.yaml from previous commit"

This creates a commit that restores the selected file while leaving other changes untouched.


Common Mistakes When Using Git Revert

While git revert is a safe command for undoing changes, developers sometimes make mistakes that lead to confusion or unexpected results. Understanding these common issues can help you avoid incorrect reversions and maintain a clean commit history.

Reverting the wrong commit

One of the most common mistakes is reverting the wrong commit. Since Git uses commit hashes to identify commits, selecting the wrong hash can revert unrelated changes.

To avoid this problem, always inspect the commit history before running the revert command.

text
git log --oneline

Example output:

text
c4f8a7d Fix configuration bug
a8c21f1 Update deployment script
e12a9f3 Initial commit

Before reverting a commit, confirm that the commit hash corresponds to the change you want to undo.

You can also review the commit details:

text
git show <commit-hash>

This ensures that you revert the correct change.

Confusion between revert and reset

Another common mistake is confusing git revert with git reset.

These commands behave very differently:

CommandBehaviorImpact on History
git revertCreates a new commit that reverses previous changesPreserves history
git resetMoves the branch pointer to an earlier commitRewrites history

Example using revert:

text
git revert HEAD

This safely undoes the latest commit while keeping the commit history intact.

Example using reset:

text
git reset --hard HEAD~1

This removes the latest commit entirely and rewrites the branch history.

For shared repositories, using git revert is usually the safer option.

Reverting merge commits incorrectly

Reverting merge commits incorrectly is another common issue. Merge commits have multiple parents, and Git needs to know which parent branch should be treated as the mainline.

Attempting to revert a merge commit without specifying a parent may result in errors.

Incorrect command:

text
git revert <merge-commit>

Correct command:

text
git revert -m 1 <merge-commit>

The -m option specifies which parent branch should remain after the revert.

Choosing the wrong parent may produce unexpected repository changes, so it is important to verify the merge structure before reverting.


Frequently Asked Questions

1. What does git revert do?

The git revert command creates a new commit that reverses the changes introduced by a previous commit while preserving the repository history.

2. How do I revert the latest commit in Git?

You can revert the most recent commit using the command git revert HEAD, which creates a new commit that cancels the changes from the latest commit.

3. Can git revert undo multiple commits?

Yes. You can revert multiple commits using git revert <commit1> <commit2> or revert a range of commits using git revert <old-commit>..<new-commit>.

4. How do you revert a merge commit in Git?

To revert a merge commit, use git revert -m 1 <merge-commit-hash> and specify the parent branch that should remain.

5. What happens if you revert a revert commit?

Reverting a revert commit restores the original changes because Git applies the reverse of the revert operation.

Summary

The git revert command provides a safe and reliable way to undo changes introduced by previous commits while preserving the complete commit history. Instead of deleting commits, Git creates a new commit that reverses earlier modifications, making it ideal for collaborative projects and shared repositories.

In this guide, we explored how git revert works internally, examined its syntax and options, and demonstrated practical scenarios such as reverting single commits, multiple commits, ranges of commits, merge commits, and pushed commits. We also covered advanced use cases like reverting a revert commit, applying reverts without committing immediately, and restoring specific file changes.

Understanding these workflows helps developers safely undo problematic changes without disrupting the repository history.


Official Documentation

For additional details about the git revert command and related Git functionality, refer to the official Git documentation:

Steve Alila

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.