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

How to revert a file 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.


The git revert command is a tool for undoing changes in a Git repository. This guide will explain how to use the git revert command to revert changes made to a specific file, preserving the integrity of your project's history.

Git revert is designed to create a new commit that reverses the effect of one or more previous commits. It is a safe method to undo changes because it doesn't alter the existing project history, making it ideal for shared repositories.

First, find the commit that introduced the changes you want to revert. You can view the commit history by using:

Terminal
git log --oneline

Look through the commit messages to identify the commit where the unwanted changes were made.

To revert the changes introduced by a specific commit to a file, use the git revert command followed by the commit hash. This command will automatically create a new commit that undoes the changes:

Terminal
git revert <commit-hash>

Replace <commit-hash> with the hash of the commit you want to revert. Git will prompt you to edit the commit message for the revert commit. Save and close the editor to proceed.

If the revert process results in conflicts (because the changes in the commit are intertwined with changes made in subsequent commits), Git will pause the revert and ask you to resolve the conflicts. After resolving any conflicts manually, you need to mark them as resolved by staging the resolved files:

Terminal
git add <file-path>

Then, continue the revert with:

Terminal
git revert --continue

For more details, see this guide on resolving Git merge conflicts.

After the revert commit is created, review the changes to ensure everything is as expected. You can see the effects of the revert by checking the status or by using git diff. If everything looks correct, push the changes to the remote repository:

Terminal
git push origin <branch-name>

Replace <branch-name> with the name of the branch you are working on.

For further reading 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