Checking out files from previous Git commits

Greg Foster
Greg Foster
Graphite software engineer

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.

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:

    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:

    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:
    git revert <commit-sha>
    If the commit includes changes to multiple files and you only want to revert the changes made to one file:
    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.

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:

    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:

    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.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2