Sub topic 6
Restoring a file from an earlier commit
Prerequisites
This chapter assumes you can use add, commit, status, and log. It also assumes you know the three areas: the working tree, the index, and HEAD. The goal is to bring back one file as it looked in an older commit, without moving your branch.
How `--source` works
By default, git restore copies a file from the index (or from HEAD, when you use --staged). The --source option changes where it copies from. You can name any commit, branch, or tag.
git restore --source=HEAD~2 -- app.pyThis writes app.py as it was two commits before HEAD into your working tree. The branch, HEAD, and the history do not change. The index does not change either, so git status shows app.py as modified but not staged. You can review the result, then stage and commit it like any other edit.
The -- separates options from file paths. It stops Git from mistaking a file name for a branch name.
Choosing where the file lands
Add --staged or --worktree to control which area receives the old version.
| Command | Working tree | Index |
|---|---|---|
| `git restore --source=HEAD~2 -- app.py` | Replaced | Unchanged |
| `git restore --source=HEAD~2 --staged -- app.py` | Unchanged | Replaced |
| `git restore --source=HEAD~2 --staged --worktree -- app.py` | Replaced | Replaced |
A worked example
Suppose a refactor broke app.py, and the version from two commits ago was fine.
git log --oneline -- app.py
git diff HEAD~2 -- app.py
git restore --source=HEAD~2 -- app.py
git add app.py
git commit -m "Restore app.py to its earlier working version"The first command lists only the commits that touched app.py, so you can pick the right one. The second shows what will change. The restore, add, and commit then record the old content as a new commit. History moves forward, so this is safe on a shared branch.
To use a commit hash, branch, or tag, replace HEAD~2. For example, --source=v1.0 or --source=a1b2c3d both work.
Common errors
- Lost edits. Restoring overwrites uncommitted changes in that file with no prompt. Commit or stash first if you might want them.
- Missing path. If the file did not exist in the chosen commit, Git reports that the path did not match any known file. Check the commit with
git log --oneline -- app.py. - Missing
--. If a file and a branch share a name, Git may read the wrong one. Put--before the path. - Wrong commit counting.
HEAD~2is relative to where HEAD is now. After new commits, the same expression points somewhere else. Use a hash when you need a fixed target. - Deleted files. A file removed in a later commit can be recreated by restoring from a commit before the deletion.
git log --diff-filter=D -- app.pyfinds the deleting commit, and its parent (<hash>~1) still has the file.
Exercise
You ran git restore --source=HEAD~3 --staged -- config.yml. What does git status show, and is your working copy of config.yml changed?
Show answer
The index now holds the version from three commits back, so git status shows the file as staged. Your working copy is unchanged, so it also appears as modified but unstaged, because it differs from the new index. To change the working copy too, add --worktree.