Git Revert Explained: Safely Undoing Your Changes

7 min read

Introduction

A version control system such as Git can ease a developer’s life, providing an opportunity to manage and track a project's source code. As a collaborator to a project, chances are that you will make a bad commit every once in a while—this is inevitable. In fact, some of the frequently asked questions by Git and GitHub users include “How do I undo a commit I made in Git?” and “How do I revert the commit pushed to GitHub?” That’s why it’s crucial to understand some source code commands such as git revert.

Git revert is a source code control command typically known as a way to “undo” commits in Git, but it goes beyond that. Think of git revert as a “get out of jail free card” whenever you make a mistake while collaborating on a project. 

This article will look at what revert means in the context of Git's workflow and the different usage scenarios, using code samples liberally. We will also look at other command line options and how this relates to resetting, amending, or reverting multiple commits.

What Is Git Revert?

You use git revert to record some new commits to reverse the effect of some earlier commits (possibly faulty ones). It is an “undo” command, but technically it is much more than that. Git revert does not delete any commit in this project history. Instead, it inverts the changes implemented in a commit and appends new commits with the opposite effect. This process helps Git remove the unwanted commit from the codebase and retain the history of every commit and the reverted one. This makes it a handy command, especially when collaborating on a project.

For instance, let’s say you’re working on a project and have made four commits so far today. Suddenly, a few moments before making the fifth commit, you realize there’s a bug in the project. After examining, you learn the bug was introduced to the project in the first commit you made that day. In a situation like this, instead of manually trying to locate the bug—which, of course, will take more time—you can undo just the commit that introduced the bug by using git revert. This process will revert only the commit with the bug and create a safe way to proceed.

How Git Revert Works 

Git revert undoes changes in a project commit history without tampering with it. When reverting, this operation takes the specific commit, inverts the changes from that commit, and implements a new reverse commit—only removing the changes tied to the reverted commit. Reverting occurs in the Git CLI; you will need to use the git revert command, which looks like this.

git revert <commit hash>

Using this command alone won’t do anything unless you specify the commit hash or reference (45111a). When you revert a previous commit, the command only removes the changes associated with the commit hash/ID specified. This does not affect any other commits you may have made before or after that particular commit.

As seen in the image above, the command reverted the specified commit, leaving the reverted changes in the staged index. This allows you to inspect the changes reverted and implement new ones if needed. 

Other Important Options

There are other ways in which one can implement the git revert command according to your use case. These include:

 --edit or -e

To edit the commit message before committing a reverted change, simply add --edit to the git revert command. Git revert will let you edit the commit message before committing the revert, although this is the default option and doesn't need to be specified.

git revert --edit 45111a

--no-edit

This command is the opposite of the --edit command. If you don’t wish to change your commit message, you can skip right over that step by adding the --no-edit command to the git revert command. This will stop the commit message editor from popping up. 

git revert --no-edit 45111a

--no-commit or -n

Typically, when you run a git revert command, Git creates commits with commit log messages stating which commits were reverted. If you wish to add more changes before committing reverted changes, simply add --no-commit or -n to the revert message. This will prevent git revert from automatically creating a new commit; instead, the revert will appear in the staging index without a commit message. This command comes in handy when reverting more than one commit’s effect in the working tree.

git revert --no-commit 45111a

Set of Commits

When reverting a set of commits, you can add .. between two Git revisions. Make sure the older commit comes first, followed by the newer commit. This will revert both commits as well as commits that exist between them.

git revert HEAD~5..HEAD~2 45111a

Git Revert vs. Reset

The net effects of the git revert command are similar to git reset. The biggest difference between them is the approach. While git revert only removes a specific commit, git reset serves as a reset feature. It allows you to go back to a previous commit, removing any other commits on its way back. Unlike git revert, git reset will roll back the state of the branch to an earlier state, dropping every commit made, which affects the history of that project codebase.

Imagine you made 30 commits to a project you are collaborating on. Just a week later, you realize there is a bug in the project from the first commit you made. If you consider using git reset in this situation to undo that first commit, Git will remove all 29 commits, taking you back to the first commit staged. 

When collaborating with a team ​​on a project, your team members will certainly need to pull code changes you’ve made and update them to their forked repository. Using git revert is the most favorable option for you and the team in a scenario like this. That’s because the Git workflow tracks every additional commit at the end of a branch. Using git reset will skip some of the additional commits. Git reset is considered to be a more destructive option because it doesn’t save any history of the commits deleted while resetting.

Conclusion

A common scenario among developers which prompts the use of git revert occurs when a team member accidentally commits something and it gets merged into a shared branch. Git revert will save you from losing your work while removing bugs that may have been introduced into a project through a commit. Git revert is a handy command, especially for developers and the DevOps team. It saves a lot of time when there is a need to roll back small, historic changes without erasing commits and updates that have subsequently been made in the project. Compared to git reset, git revert is the best alternative for developers, especially those who are collaborating on a project. You’ll need tools like git revert and other “undo” commands, such as git checkout and git reset, to undo any changes in your Git repository. However, it is best if you understand what each one of the commands does individually.


This post was written by Anita Ihuman. Anita is a software developer with experience working with React (Next.js, Gatsby). Skilled in technical blogging and public speaking, she also loves contributing to open source projects. Anita is the community manager at layer5 and a content creator for the Gnome Africa Blog.

Stay up to date

We'll never share your email address and you can opt out at any time, we promise.