Git
Git Stash
Version Control
Unstash Files
Software Development

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

bash
1# List all stashes
2git stash list
3# stash@{0}: WIP on main: abc1234 Latest commit
4# stash@{1}: On feature: work in progress
5
6# Show files changed in the most recent stash
7git stash show
8#  src/app.js     | 15 +++++++++------
9#  src/config.js  |  3 +++
10#  tests/test.js  | 22 ++++++++++++++++++++++
11
12# Show the full diff of the stash
13git stash show -p
14
15# Show a specific stash
16git stash show stash@{1}

Method 1: git checkout from Stash (Git < 2.23)

bash
1# Extract a single file from the stash
2git checkout stash@{0} -- src/config.js
3
4# Extract multiple specific files
5git checkout stash@{0} -- src/config.js tests/test.js
6
7# The file is now staged (in the index) and in the working tree
8git status
9# Changes to be committed:
10#   modified: src/config.js
11#   modified: tests/test.js

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+)

bash
1# Restore a file from the stash to the working tree
2git restore --source stash@{0} -- src/config.js
3
4# Restore to both working tree and staging area
5git restore --source stash@{0} --staged --worktree -- src/config.js
6
7# Restore multiple files
8git restore --source stash@{0} -- src/config.js tests/test.js

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.

bash
1# Show the stash diff for a specific file and apply it
2git diff stash@{0}^..stash@{0} -- src/config.js | git apply
3
4# Or use git stash show with path filtering
5git stash show -p stash@{0} -- src/config.js | git apply
6
7# Apply changes to multiple files
8git stash show -p stash@{0} -- src/config.js src/app.js | git apply

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.

bash
1# Apply specific hunks from the stash interactively
2git stash show -p stash@{0} | git apply --3way --index
3
4# Or cherry-pick hunks with git checkout -p
5git checkout -p stash@{0}
6# Git prompts for each hunk: Apply this hunk [y,n,q,a,d,e,?]?

Drop the Stash After Extracting

Since extracting individual files does not remove the stash, drop it manually when you are done.

bash
1# Drop the most recent stash
2git stash drop stash@{0}
3
4# Or drop a specific stash
5git stash drop stash@{1}
6
7# Clear all stashes
8git stash clear

Stashing Only Specific Files

To create a stash with only certain files in the first place:

bash
1# Stash specific files (Git 2.13+)
2git stash push -m "config changes" -- src/config.js src/settings.js
3
4# Interactive stash — select hunks
5git stash push -p -m "partial changes"
6
7# Stash including untracked files
8git stash push -u -- src/new_file.js

Common Pitfalls

  • git checkout stash@{0} -- file overwrites 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. Run git stash drop when you no longer need the stash.
  • Conflicts during patch apply: git apply fails silently if the patch does not apply cleanly. Use git apply --3way to 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 was stash@{1} becomes stash@{0}. Scripts that reference stashes by index may apply the wrong stash. Use git stash list to verify before applying.
  • Forgetting the -- separator: Without --, Git may interpret filenames as branch names. Always use git checkout stash@{0} -- path/to/file or git restore --source stash@{0} -- path/to/file with 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} -- file for older Git versions
  • Use git stash show -p -- file | git apply for patch-based selective application
  • Extracting individual files does not drop the stash — use git stash drop when done
  • Use git stash push -- file1 file2 to stash only specific files in the first place

Course illustration
Course illustration

All Rights Reserved.