Read Anthropic’s case study about Graphite Reviewer

Checking out files from previous Git commits

Greg Foster
Greg Foster
Graphite software engineer


Note

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


Sometimes, you may need to revert or checkout a file to its state at a previous commit without disturbing other files or the current project state. This guide will walk you through various methods to checkout, revert, or reset a specific file to a previous commit's state in Git.

  • git checkout: This command is used to switch branches or restore working tree files. It can also be used to checkout files from a specific commit to your working directory.
  • git revert: This command is used to reverse changes that were made by previous commits. It can target specific commits and is safe for use in public branches because it preserves the history.
  • git reset: This command is used to reset the index and working directory to a specific state. If you need to undo changes in your staging area and working directory, git reset can be applied to specific files.
Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

If you want to restore a file from a previous commit, you can use the git checkout command.

  1. Identify the commit SHA: First, find the commit SHA (a unique identifying code for each commit) of the version of the file you want to restore. You can find this by using:

    Terminal
    git log -- path/to/file

    This command will show a list of commits that affected the specified file. Note the SHA of the commit from which you want to restore the file. This will show you the commit history of the specified file.

  2. Checkout the file: Once you have the commit SHA, use the following command to checkout the file:

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

    Replace <commit-sha> with the actual SHA of the commit and path/to/file with the actual path to the file.

This command will restore the file to its state at the specified commit without changing the current branch or affecting other files.

Sometimes, instead of checking out to a previous commit, you might want to revert the changes made to a file in a specific commit.

  1. Use git revert with the commit SHA: If you want to undo the changes made to a file in a specific commit and this commit only changed that one file, you can run:
    Terminal
    git revert <commit-sha>
    If the commit includes changes to multiple files and you only want to revert the changes made to one file:
    Terminal
    git revert --no-commit <commit-sha>
    git reset HEAD
    git add path/to/file
    git commit -m "Reverted changes to path/to/file"

This method will create a new commit on your current branch, reverting the changes made to the specified file.

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

If you want to undo changes to a file that you haven't committed yet or remove changes from the staging area, you can use the command git reset.

  1. Reset a specific file:

    Terminal
    git reset <commit-sha> -- path/to/file

    This command will unstage the file from the specified commit but keep the changes in your working directory. From here you can make safely make further changes to the file.

  2. Discard changes completely:

    Terminal
    git checkout HEAD -- path/to/file

    This is useful if you want to discard changes to a file and revert it to the state it was in at the last commit. Use caution as this is a potentially destructive command, as it will discard your local changes.

For more information on checking out a file from a previous commit, 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