How to use git revert

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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](<https://graphite.dev/guides/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:

Terminal
commit 3a410e80809fa326b2c93d65f7bbbde5f8395b95
Author: Alex Doe <alex.doe@example.com>
Date: Wed Apr 12 14:23:40 2023 -0400
Add new feature to the project
commit cf23df2207d99a74fbe169e3eba035e633b65d94
Author: Jane Smith <jane.smith@example.com>
Date: Tue Apr 11 16:17:20 2023 -0400
Update documentation
commit e8577e31bb9e2773a4a7cda8a799bc238bd12156
Author: Max Doe <max.doe@example.com>
Date: Mon Apr 10 08:24:13 2023 -0400
Initial commit of the project
  1. Commit SHA-1 Hash:

    • Example: 3a410e80809fa326b2c93d65f7bbbde5f8395b95
    • This is a unique identifier for each commit. It is a SHA-1 hash that Git generates based on the contents of the commit (which includes the changes, author, and date). This hash can be used to reference the commit in various Git commands.
  2. Author:

    • Example: Author: Alex Doe <alex.doe@example.com>
    • This line shows the name and email address of the user who made the commit. It is useful for tracking who made specific changes in a collaborative project.
  3. Date:

    • Example: Date: Wed Apr 12 14:23:40 2023 -0400
    • The date and time when the commit was made. The timezone is also included (e.g., -0400 indicates the Eastern Daylight Time).
  4. Commit Message:

    • Example: Add new feature to the project
    • A brief description of the changes made in the commit, written by the author. This message helps other contributors understand the purpose of the changes.

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>

  1. 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: Recursive revert message

  • 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:

Terminal
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.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2