How would I extract a single file (or changes to a file) from a git stash?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To extract a single file from a git stash without applying the entire stash, use git checkout (or git restore in Git 2.23+) to pull that specific file from the stash reference into your working directory.
Both commands replace the file in your working directory with the version saved in the stash. The rest of the stash remains untouched, and the stash itself is not dropped.
Understanding Stash References
A git stash is internally a commit object. When you run git stash push, Git creates a commit that captures your working directory changes (and optionally staged changes and untracked files). These commits are stored in a stack:
stash@{0}is the most recent stash.stash@{1}is the one before that.stash@{N}is the Nth stash in the stack.
List all stashes with:
Because stashes are commits, you can use them anywhere Git expects a commit reference. This is what makes it possible to extract individual files, diff against them, or cherry-pick specific changes.
Method 1: Restore a File From Stash (Replace Working Copy)
This overwrites the file in your working directory with the stashed version:
After running this, the file in your working directory matches exactly what was in the stash. Your other files are unaffected. The stash remains in the stack.
To restore multiple files at once:
To restore an entire directory:
Method 2: View Changes Before Extracting
Before overwriting your working copy, you may want to see what the stash contains for that specific file.
Show the diff for a single file
This shows the difference between the stashed version and your current HEAD. If you want to see the diff against your working directory instead:
Show the full content of the stashed file
This prints the entire file content as it was when stashed. Useful for reviewing before extracting.
List all files modified in a stash
With stats:
Method 3: Apply Changes as a Patch (Merge, Not Replace)
git restore and git checkout replace the file entirely. If you want to merge the stashed changes into your current version of the file (preserving your current edits), create a patch and apply it:
The stash@{0}^..stash@{0} range compares the stash's parent (the commit the stash was created from) with the stash itself, giving you only the changes that were stashed.
If the patch conflicts with your current changes, git apply will fail. You can use --3way to attempt a three-way merge:
This creates conflict markers in the file that you resolve manually, similar to a merge conflict.
Method 4: Interactive Stash Apply With git stash pop -p (Partial)
If you want to interactively select which hunks from a stash to apply, you can use the patch mode. This does not limit to a single file but lets you pick individual change hunks:
For applying only specific hunks, save the diff and use git apply with manual editing:
This applies only the changes to path/to/file.txt from the stash, as a patch against your current working directory.
Command Reference
| Task | Command |
| List all stashes | git stash list |
| List files in a stash | git stash show stash@{N} --name-only |
| View stashed file content | git show stash@{N}:path/to/file |
| Diff stash vs HEAD for one file | git diff stash@{N} -- path/to/file |
| Extract file (replace) | git restore --source stash@{N} -- path/to/file |
| Extract file (legacy) | git checkout stash@{N} -- path/to/file |
| Apply stash changes as patch | git diff stash@{N}^..stash@{N} -- file > f.patch |
| Apply entire stash | git stash apply stash@{N} |
| Apply and drop stash | git stash pop stash@{N} |
Working With Staged vs. Unstaged Changes in Stash
When you stash changes, Git captures both staged and unstaged modifications. By default, git stash show and file extraction commands see the combined result. If you stashed with --keep-index or want to distinguish between staged and unstaged parts, the stash commit structure matters:
stash@{0}is the working directory state (unstaged changes).stash@{0}^2is the index state (staged changes).
To extract only the staged version of a file:
This is rarely needed but useful when you stashed a mix of staged and unstaged work and want to separate them during extraction.
Common Pitfalls
Using git checkout stash@{0} -- file without first checking what the stashed version contains can overwrite uncommitted local changes. Always run git diff stash@{0} -- file or git show stash@{0}:file first to review the stash content.
Forgetting that git restore and git checkout replace the file entirely rather than merging changes leads to lost work. If your working copy has modifications you want to keep, use the patch approach instead.
Referencing the wrong stash index is easy when stashes accumulate. Run git stash list and verify the stash message before extracting. After you git stash drop an entry, all subsequent stash indices shift down by one.
Assuming git stash pop can target a single file is incorrect. pop always applies the entire stash. For single-file extraction, use restore, checkout, or the patch method.
Stashing without a message (git stash instead of git stash push -m "description") makes it difficult to identify the right stash later. Always include a descriptive message.
Extracting from a stash that was created on a different branch can introduce conflicts if the file has diverged. The patch approach with --3way handles this more gracefully than a direct file replacement.
Summary
- Use
git restore --source stash@{0} -- fileto extract a single file from a stash. - Use
git show stash@{0}:fileto view the stashed file content before extracting. - Use
git diff stash@{0} -- fileto review changes before applying. - Use the patch approach (
git diff stash@{0}^..stash@{0} -- file | git apply) when you want to merge stash changes with your current work instead of replacing. - Always verify the stash index with
git stash listbefore extraction. - The stash is not dropped after extraction. Drop it explicitly with
git stash dropwhen you no longer need it.

