How can I reset or revert a file to a specific revision?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Restoring one file to an earlier Git revision is a common task during bug fixes, refactors, and incident response. The important detail is deciding whether you want a local workspace change only or a new commit that records the rollback in history. Once that choice is clear, the correct command sequence is straightforward and safe.
Choose the Right Goal First
Before typing commands, decide which outcome you need.
- You want to preview or test an old version in your working tree.
- You want to commit a rollback of one file on your current branch.
- You want to undo a bad commit in shared history without rewriting branch history.
Git has commands for all three, but they are not interchangeable.
Restore a File in Working Directory Only
If you want the file content from an old commit but do not want to create a commit yet, use git restore with a source revision.
This updates your working tree copy of that file. If you also want it staged immediately, add --staged.
Older Git versions used git checkout for this task.
Both approaches replace that file with the chosen historical snapshot.
Find the Correct Revision for the File
A common mistake is restoring from the wrong commit hash. Inspect file history first.
If you need only deletion-related history:
To inspect what the file looked like at a specific commit without modifying your tree:
This quick check prevents accidental rollback to an unrelated intermediate state.
Commit a File-Level Rollback
If you want branch history to record the change, restore the file, stage it, and commit.
This is usually the safest pattern in team branches because it creates an explicit audit trail.
Revert a Commit Versus Restore a File
git revert and git restore solve different problems.
git restorecopies file content from another revision.git revertcreates a new commit that inverses a full commit.
If a bad commit changed many files but you only want to undo one file, use file restore plus commit. If you need to reverse the entire commit in shared history, git revert is usually the right choice.
For merge commits, git revert has additional parent selection complexity, so file-level restore is often simpler when scope is limited.
Recover a Deleted File
If file was deleted and you want it back, identify the delete commit and restore from its parent state.
You can also restore from any known commit where the file existed.
Compare Before Committing
After restoration, inspect differences before creating commit.
This is useful when file has mixed formatting or generated sections and you only intended targeted rollback.
Safer Workflow in Shared Repositories
In collaborative branches, avoid destructive history edits unless coordinated. Prefer additive commit-based rollback.
Recommended sequence:
- create short-lived branch for rollback
- restore file from specific revision
- run tests and lint
- commit with clear message
- open pull request with rationale
This keeps review and blame history clear for future debugging.
Common Pitfalls
A frequent pitfall is mixing up restore and reset commands and unintentionally affecting index state. Another is restoring from a commit hash without verifying file content first, which can pull in an older-than-intended version. Teams also often forget to run tests after rollback, especially when file interacts with config, serialization, or migrations. Finally, rewriting remote shared history to fix one file usually creates more risk than a normal rollback commit.
Summary
- Decide first whether you need local restore or committed rollback.
- Use
git restore --source=... -- pathfor file-level retrieval. - Check file history and preview content before restoring.
- Commit restored file for safe collaboration and traceability.
- Prefer additive rollback commits over destructive history rewrites on shared branches.

