How to revert a specific file to a previous version in Git

· Category: Git

Short answer

Use git restore --source=<commit> -- <file> (Git 2.23+, recommended) or the legacy git checkout <commit> -- <file> to restore a specific file to the state it had in a previous commit.

Steps

  1. Find the commit hash:
git log --oneline -- file.txt
  1. Restore the file:
git restore --source=abc1234 -- file.txt   # Git 2.23+
# or, on older Git:
git checkout abc1234 -- file.txt
  1. Stage and commit the restoration:
git add file.txt
git commit -m "Restore file.txt to previous version"

Tips

  • This creates a new commit with the old file content.
  • git restore was introduced in Git 2.23 (Aug 2019) specifically to take over the file-restoration role of the overloaded git checkout.
  • Restoring a file does not affect other files or branch history.

Common issues

  • Forgetting to commit leaves the file staged but uncommitted.
  • Make sure you are checking out from the correct commit hash.