Data report"State of code review 2024" is now liveRead the full report

How to revert to a previous commit in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


Reverting changes to a previous commit in Git is useful when you need to undo changes that have been made since the time of a certain commit. This might be necessary for removing introduced bugs, undoing a feature, or simply resetting the project to a stable state. This guide covers how to revert to previous commits in Git, including entire commits or specific files, using commands like git revert to previous commit and more.

The git revert command generates a new commit that undoes the changes made in one or more existing commits. This is a safe way to undo changes, as it doesn't alter the project's commit history. Instead, it adds a new commit on top that reverses the effect of earlier commits.

Here are some reasons you might find yourself needing to revert to a previous commit:

  • Bug introduction: If a recent commit introduces errors or bugs, reverting can restore the last known good state.
  • Feature rollback: Rolling back features that are no longer needed or that were added prematurely.
  • Merge mistakes: Correcting issues caused by incorrect merges.

First, identify the commit to which you want to revert. You can view the commit history using:

Terminal
git log

This command shows a list of recent commits, including their commit IDs, author, date, and the commit message. Find the commit ID of the commit just before the point you want to revert to.

To revert the entire repository to the state of a previous commit, use:

Terminal
git revert --no-commit <commit-id>..HEAD
git commit

Replace <commit-id> with the ID of the commit you're reverting to. This sequence of commands reverts all changes from the current HEAD back to the specified commit.

If you only need to revert changes to specific files, you can do so by checking out those files from a specific commit:

Terminal
git checkout <commit-id> -- path/to/file

This command will bring the specified files back to their state at <commit-id>.

To revert a range of commits, you can use:

Terminal
git revert --no-commit <commit-id>^..HEAD
git commit

This command reverts all changes made from the commit just before <commit-id> to the current HEAD.

After reverting locally, you'll likely need to push your changes to the remote repository:

Terminal
git push origin main

This updates the remote repository with your revert commits.

  • Merge conflicts: Reverting changes can lead to merge conflicts, especially if subsequent changes have been made to the same parts of files. Resolve these conflicts as you would during a merge.
  • Reverting a revert: If you need to undo a revert, you can use git revert again on the revert commit itself.

For further reading on reverting commits see the official Git documentation.

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