How to unstash only certain files?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
git stash pop and git stash apply restore all stashed changes at once. When you only need specific files from a stash, Git provides several approaches: git checkout (or git restore) with a stash reference to extract individual files, git stash show -p piped to git apply with path filtering, or git diff to create a selective patch. The stash itself remains intact when you extract individual files (unlike pop, which drops the stash), so you can pull files from it multiple times.
View What Is in the Stash
Method 1: git checkout from Stash (Git < 2.23)
This overwrites the current version of the file with the stashed version. The stash is not dropped.
Method 2: git restore from Stash (Git 2.23+)
git restore is the modern replacement for git checkout for file operations.
Method 3: Patch-Based Selective Apply
Extract changes as a patch and apply only specific files.
The patch approach applies the diff (additions and deletions) rather than overwriting the entire file. This is useful when the file has changed since stashing.
Method 4: Interactive Stash Apply
Use git stash show -p with git apply interactively to select specific hunks.
Drop the Stash After Extracting
Since extracting individual files does not remove the stash, drop it manually when you are done.
Stashing Only Specific Files
To create a stash with only certain files in the first place:
Common Pitfalls
git checkout stash@{0} -- fileoverwrites without merging: This replaces the current file content entirely with the stashed version. If the file has been modified since stashing, those changes are lost. Use the patch method (git stash show -p | git apply) to merge changes instead of overwriting.- Stash not dropped after partial extraction: Unlike
git stash pop, extracting individual files does not remove the stash. Forgetting to drop it leads to stash accumulation. Rungit stash dropwhen you no longer need the stash. - Conflicts during patch apply:
git applyfails silently if the patch does not apply cleanly. Usegit apply --3wayto fall back to three-way merge, which creates conflict markers that you can resolve manually. - Stash index shifting after drop: After dropping
stash@{0}, what wasstash@{1}becomesstash@{0}. Scripts that reference stashes by index may apply the wrong stash. Usegit stash listto verify before applying. - Forgetting the
--separator: Without--, Git may interpret filenames as branch names. Always usegit checkout stash@{0} -- path/to/fileorgit restore --source stash@{0} -- path/to/filewith the double-dash separator to avoid ambiguity.
Summary
- Use
git restore --source stash@{0} -- file(Git 2.23+) to extract specific files from a stash - Use
git checkout stash@{0} -- filefor older Git versions - Use
git stash show -p -- file | git applyfor patch-based selective application - Extracting individual files does not drop the stash — use
git stash dropwhen done - Use
git stash push -- file1 file2to stash only specific files in the first place

