In software development, changes made to a project often need to be undone, either due to mistakes or the need to roll back specific features. Git provides a robust toolset to revert changes without altering the entire history of your project. While many Git commands allow you to undo changes (such as reset or checkout), the git revert command is unique because it undoes changes in a safe and controlled manner by creating a new commit that reverses previous changes, leaving the history intact.
In this chapter, we will explore how to revert changes in Git, from basic to advanced scenarios, with detailed examples. We will also discuss the difference between revert
and other Git commands, giving you the confidence to handle any situation.
Reverting in Git is the process of creating a new commit that undoes the changes introduced by a previous commit (or series of commits). Instead of removing a commit from history (as reset
or rebase
might), git revert
safely negates the effects of a commit while preserving the project’s commit history.
This makes it a useful tool for team-based projects where preserving the history is essential for collaboration, auditing, or future reference.
You should consider using git revert
in the following scenarios:
Let’s begin with the most basic scenario of reverting a single commit.
To revert a single commit, first identify the commit you want to revert using git log
. Here’s how to check the commit history:
$ git log --oneline
This will list all commits in the repository, showing the commit hash and message:
a1b2c3d Fourth commit
9e8f7g6 Third commit
5d4f3e2 Second commit
7a6b5c1 First commit
To revert a commit, use the git revert
command followed by the commit hash:
$ git revert a1b2c3d
This creates a new commit that undoes the changes made by the commit a1b2c3d
. After running the command, Git will prompt you to write a commit message for the revert. The default message will look something like this:
Revert "Fourth commit"
This reverts commit a1b2c3d.
Once you save the commit message, the changes introduced by a1b2c3d
will be undone, but the history will remain intact.
Revert "Fourth commit"
This reverts commit a1b2c3d.
After running git revert
, you can verify that the changes were reverted by using git log
again:
$ git log --oneline
You’ll see a new commit like this:
f7e6d5c Revert "Fourth commit"
a1b2c3d Fourth commit
The commit history shows that the revert action created a new commit, but the original commit a1b2c3d
is still part of the history. This makes git revert
a non-destructive command.
Sometimes, you may need to revert several commits at once. You can either revert them one by one or use a range of commits.
To revert multiple commits, you can run git revert
on each commit individually:
$ git revert
$ git revert
$ git revert
Each command creates a new revert commit, undoing the respective changes.
If you have a range of consecutive commits to revert, Git allows you to revert them in one command using a range specification. For example, to revert the last three commits:
$ git revert HEAD~3..HEAD
This command will revert the last three commits. Git will create individual revert commits for each one, and you will be prompted to write a message for each revert.
[revert] Revert "Third commit"
[revert] Revert "Second commit"
[revert] Revert "First commit"
Afterward, when you check the commit log, you’ll see three new commits that undo the respective changes:
1234567 Revert "Third commit"
2345678 Revert "Second commit"
3456789 Revert "First commit"
Reverting a merge commit is more complex because a merge commit represents the combination of two branches. Git will ask which parent branch’s changes should be reverted.
Suppose you have the following history:
A---B---C---M
\ /
D--E
Here, M
is the merge commit where branch E
was merged into branch C
. If you want to revert M
, run:
$ git revert -m 1 M
The -m
flag specifies the parent to revert. If you pass -m 1
, Git will undo the changes from the second branch (the one that was merged in).
If you want to see the effects of a revert before committing it, you can pass the --no-commit
option. This allows you to stage the changes without committing them immediately.
$ git revert --no-commit
This command will apply the changes but not automatically create a commit. You can review the changes and either commit them or further modify them before committing.
At this point, it’s important to understand the difference between git revert
, git reset
, and git checkout
as all of them are used to undo changes, but in different ways.
git revert
: Creates a new commit that undoes a previous commit while keeping the commit history intact. Safe for collaborative projects.
git reset
: Moves the branch pointer backward, undoing commits by removing them from the history. This can rewrite history, making it unsafe for shared branches.
git checkout
: Used to switch between branches or restore files to a previous state. It doesn’t rewrite history or undo commits, but it allows you to explore or recover previous versions of files.
Let’s assume you have the following commit history:
A---B---C---D (HEAD)
git revert C
: A new commit will be created to undo the changes from commit C
, resulting in:
A---B---C---D---R (HEAD)
git reset --hard B
: The commit history will be rewritten, discarding C
and D
:
A---B (HEAD)
git checkout B
: You will move to the state of commit B
in a detached HEAD state, but no history will be rewritten:
A---B (detached HEAD)
When working on a shared repository, prefer using git revert
over git reset
to avoid rewriting history. Reverting maintains transparency and ensures other collaborators won’t face conflicts with their work.
If possible, test the revert in a separate branch to ensure it behaves as expected before applying it to the main branch. You can create a temporary branch, revert the changes, and test the result.
$ git checkout -b temp-branch
$ git revert
Once you’re satisfied, you can merge the revert commit into the main branch.
Git automatically generates a commit message when reverting, but it’s a good practice to add more context. For example:
Revert "Add new login feature"
This reverts commit due to a security vulnerability in the login logic.
Reverting changes in Git is a safe and transparent way to undo mistakes or back out of unwanted features while maintaining a clean and traceable commit history. By understanding how to use git revert, you can selectively undo changes without affecting the integrity of your project. This command is essential in collaborative environments where rewriting history could cause conflicts and confusion. Happy coding !❤️