How can I restore one file from an earlier Git commit?
Quick answer
To restore one file from an earlier commit, first run git status and inspect any changes to that file. Preserve edits you may need, for example by copying the file outside the repository. Then run git restore --source <commit> -- <path>. This updates the file in your working tree; it does not move your branch. Check the result and status before deciding what to do next. A file absent from the chosen commit may be removed.
The question, the details, the next step.
A practical checklist
Identify the intended earlier commit and exact file path.
Run
git statusand review staged, unstaged, and untracked work.Preserve current edits you may need; status is not a backup.
Run
git restore --source <commit> -- <path>only after checking the target.Inspect the restored file and run
git statusagain.
A few more useful answers
Does restoring one file move my branch?
No. This command restores the selected path in the working tree; it does not move the branch. [S1]
Does `git status` save my current edits?
No. It reports repository state and can help you spot changes to preserve, but it does not back them up. [S2]
What does `--staged` change?
The default restore location is the working tree; --staged affects the index. Use it only when you intend to restore the index. [S1]
Sources
References used for this guide.
- [S1] Git: git-restore documentation git-scm.com · Checked
- [S2] Git: git-status documentation git-scm.com · Checked
