How to use git revert

Greg Foster
Greg Foster
Graphite software engineer

Reverting a commit in Git is a safe and easy way to undo changes that have already been committed to your repository's history. Unlike other (potentially destructive) operations such as [git reset](<>), which alters the underlying commit history, git revert creates a new commit that undoes the changes introduced by previous commits. This guide explains how to use the git revert command to rollback to previous commits.

The git revert command undoes changes from a specific commit by creating a new commit with the inverse of those changes. It's a safe way to rollback changes without rewriting commit history.

When should you use git revert?

Reverting is particularly useful when you need to undo changes in a repository or in a situation where maintaining a linear history is crucial. It keeps a record of the changes, allowing others to see the correction explicitly.

  1. Revert the last commit: To revert the most recent commit run:

    git revert HEAD

    Because the HEAD points to the commit SHA of the latest commit, by passing it in as an argument togit revert , we can create a new commit that undoes the most recent changes to the codebase.

  2. Revert a specific commit: To revert a specific commit, you need the specific commit's hash. In order to find the hash of the commit you wish to revert, you can run git log or git lg to see a condensed history of the commits on your branch. You should see something like this:

    You can then use the commit message to identify which changes you’d like to revert, and then pass the accompanying commit SHA in as an argument to the git revert command:

    git revert <commit-hash>

  3. Reverting a revert commit: If you've successfully reverted a commit but then later decide you actually want to keep those reverted changes, you can revert the revert commit using the same method listed above. Simply find the commit hash of the revert commit and then pass it in as an argument to the git revert command:

    git revert <revert-commit-hash>

    This creates a new commit that re-applies the changes of the originally reverted commit.

    You can of course revert this commit as well leading to fun commit messages like:

  • Revert without committing immediately: If you want to revert changes but not commit them right away (perhaps to make additional adjustments), you can use the --no-commit flag:

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

    This undoes the changes in your working directory (the local folder on your computer that contains all the files and subdirectories associated with your Git project) but doesn’t actually create a commit.

    You can then commit the changes of this revert yourself by running:

    git commit -m "<your-commit-message>"

  • Revert multiple commits: To revert a range of commits, you can use:

    `git revert <newest-commit-hash>^..<oldest-commit-hash>


    Note the caret (^) symbol, which means the revert range is inclusive of the commit specified by <newest-commit-hash>.

    This is especially useful if you wish to revert entire stacks of commits at once.

When you revert a commit, you might encounter merge conflicts if Git cannot automatically undo the changes. If this happens, you'll need to manually resolve the conflicts.

For more information on this step, see this detailed guide on resolving merge conflicts.

After resolving any conflicts, complete the revert by adding the changes and continuing the revert:

git add .
git revert --continue
  • Edit revert commit message: While Git provides a default commit message for revert commits, you may want to add more specific details. To modify the default commit message to add more context or clarity use the --edit flag:

    git revert --edit <commit-hash>

  • Revert on a specific branch: Always ensure you're on the correct branch before performing a revert operation to avoid unintended changes on other branches. Just like when making other changes to your repository, it’s best to branch off of main and merge your changes through a pull request.

  • Communicate when reverting: Especially when reverting commits in public repositories, communicate with your team about the revert, especially if it affects shared branches or recent work. This prevents teammates from working on stale code, and reduces the possibility of merge conflicts.

For further information, see the official git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2