Read Anthropic’s case study about Graphite Reviewer

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.


Reverting changes in Git enables you to undo modifications and return to a previous state without affecting the entire project. This guide provides a comprehensive overview of how to revert a file in Git, using various commands that can help you manage your project’s version history efficiently.

Reverting in Git means to undo changes made to the files in your repository. It's useful for discarding unwanted modifications, correcting mistakes, or simply reviewing earlier versions of a file. The process involves using Git commands that manipulate the history or the working tree of your repository.

If you've made changes to a file and have not yet committed them, you can revert these uncommitted changes using the git checkout command (for versions before Git 2.23) or the git restore command (for Git 2.23 and later).

Using git checkout:

Terminal
git checkout -- <file_path>

Replace <file_path> with the path to the file you want to revert.

Using git restore:

Terminal
git restore <file_path>

This command will restore the file to its state at the last commit, effectively discarding any changes made since then.

If the changes were committed and you need to undo those changes, you can use the git revert command. This command is used to reverse the effects of a specific commit.

Identifying the commit:

First, identify the commit from which the changes to the file were introduced. Use the git log command to see the history:

Terminal
git log -- <file_path>

Look for the commit hash of the specific change you want to revert.

Reverting the commit:

Once you have the commit hash, you can revert the changes made by that commit to the specified file using:

Terminal
git revert --no-commit <commit_hash>
git reset HEAD
git add <file_path>
git commit -m "Revert changes in <file_path>"

This series of commands reverses the effects of the specified commit but does not automatically create a new commit. You manually add the changes and commit them, ensuring you only revert changes to the specific file.

If you want to revert a file back to how it was in a specific commit, you can use the git checkout or git restore command with the commit hash.

Using git checkout:

Terminal
git checkout <commit_hash> -- <file_path>

Using git restore:

Terminal
git restore --source <commit_hash> <file_path>

After executing either of these commands, the file will revert to its state in the specified commit. You’ll need to commit this change if you want to keep it.

For more information, see this guide on using the git revert command.

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