How to revert only one file back to a revision or commit
Reverting a single file to a previous commit
You’ve accidentally broken a file and need to restore it from an earlier commit without touching anything else in your repository. Here’s how.
Restore a file to a specific commit
To restore my_file.name to its state at a specific commit:
git checkout commit_sha1 my_file.name
Replace commit_sha1 with the actual commit hash. You can find it using git log:
git log --oneline my_file.name
Using relative commit references
Instead of a full commit hash, you can reference commits relative to HEAD:
git checkout HEAD~4 my_file.name # 4 commits ago
git checkout HEAD~1 my_file.name # previous commit
git checkout HEAD^ my_file.name # equivalent to HEAD~1
Or reference relative to a branch:
git checkout master~3 my_file.name # 3 commits back on master
git checkout main~2 my_file.name # 2 commits back on main
Restore to current branch
To discard local changes and restore the file from your current branch (typically master or main):
git checkout my_file.name
This is equivalent to:
git checkout HEAD my_file.name
What actually happens
When you run these commands, git:
- Retrieves the file from the specified commit
- Places it in your working directory
- Stages the change (adds it to the index)
You still need to commit this change:
git add my_file.name
git commit -m "Restore my_file.name to previous version"
Checking the file first
Before committing, verify the restored file is correct:
git diff HEAD my_file.name
Or view it without checking it out:
git show commit_sha1:my_file.name
Common variations
Restore multiple files from the same commit:
git checkout commit_sha1 file1.txt file2.txt file3.txt
Restore all files to a previous commit (rarely needed, but possible):
git checkout commit_sha1 .
Use git restore (modern alternative):
Git 2.23+ introduced git restore as a clearer alternative:
git restore --source=commit_sha1 my_file.name
This is functionally identical to git checkout but has clearer semantics — checkout is primarily for switching branches, while restore explicitly restores files.
Using git reset for unstaged changes:
If you’ve only modified the file locally but haven’t committed bad changes:
git reset my_file.name # unstages changes
git checkout my_file.name # discards local modifications
Or in one step:
git restore my_file.name
Troubleshooting
File not found error: The file may not have existed at that commit. Check the commit:
git ls-tree -r commit_sha1 | grep my_file.name
Wrong commit restored: You can undo the restoration before committing:
git reset HEAD my_file.name
git checkout my_file.name
Then try again with the correct commit reference.