Bird
Raised Fist0
Gitdevops~15 mins

Stashing specific files in Git - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - Stashing specific files
What is it?
Stashing specific files in git means temporarily saving changes from only some files, not all, so you can work on something else without losing your progress. It lets you keep your workspace clean by hiding changes safely. Later, you can bring back those changes exactly as they were. This is useful when you want to switch tasks but only part of your work is ready to be saved.
Why it matters
Without the ability to stash specific files, you would have to stash or commit all your changes together, even if some are incomplete or unrelated. This can slow you down and clutter your project history. Stashing specific files helps you stay organized and focused, making your work smoother and less error-prone.
Where it fits
Before learning this, you should understand basic git commands like add, commit, and stash. After mastering stashing specific files, you can explore advanced git workflows like branching strategies and interactive rebasing.
Mental Model
Core Idea
Stashing specific files is like putting only certain items into a temporary box to clear your workspace while keeping other items out and accessible.
Think of it like...
Imagine you are working on a desk cluttered with papers. You want to clear space but only want to put some papers into a box temporarily, leaving others on the desk to keep working on them. Stashing specific files is like choosing which papers to box up and which to leave out.
Workspace (git repo)
┌───────────────┬───────────────┐
│ Changed files │ Unchanged files│
├───────────────┼───────────────┤
│ file1.txt     │ file3.txt     │
│ file2.txt     │ file4.txt     │
└───────────────┴───────────────┘

Stash command targets only file1.txt:

[Stash Box]
┌───────────────┐
│ file1.txt     │
└───────────────┘

Workspace after stash:

┌───────────────┬───────────────┐
│ file2.txt     │ file3.txt     │
│ (still changed)│ (unchanged)  │
└───────────────┴───────────────┘
Build-Up - 6 Steps
1
FoundationUnderstanding git stash basics
🤔
Concept: Learn what git stash does and how it saves all current changes temporarily.
The git stash command saves all your current changes (both staged and unstaged) into a hidden area. This lets you switch branches or work on something else without committing unfinished work. You can later restore these changes with git stash apply or git stash pop.
Result
Your working directory becomes clean, as if you just cloned the repo, but your changes are saved safely in the stash list.
Understanding the basic stash operation is essential before learning how to stash only some files.
2
FoundationIdentifying changed files in git
🤔
Concept: Learn how to see which files have changes that can be stashed.
Use git status to see which files are modified, staged, or untracked. This helps you decide which files you want to stash specifically.
Result
You get a clear list of changed files, helping you plan selective stashing.
Knowing your changed files is the first step to choosing which files to stash.
3
IntermediateStashing only staged or unstaged changes
🤔Before reading on: do you think git stash can stash only staged changes, only unstaged changes, or both at the same time? Commit to your answer.
Concept: Git stash can target only staged or only unstaged changes using options.
By default, git stash saves both staged and unstaged changes. Use git stash push --staged to stash only staged changes. Use git stash push --keep-index to stash unstaged changes but keep staged ones intact.
Result
You stash only the changes you want, leaving others untouched in your working directory.
Knowing how to separate staged and unstaged changes in stash helps you control exactly what you save temporarily.
4
IntermediateStashing specific files using pathspec
🤔Before reading on: do you think git stash supports specifying file names directly to stash only those files? Commit to your answer.
Concept: Git stash push supports specifying file paths to stash only those files' changes.
Use git stash push -- to stash changes only from those files. For example, git stash push -- src/app.js README.md stashes changes only in those two files.
Result
Only the specified files' changes are saved in the stash; other changes remain in the working directory.
Using pathspec with stash push gives precise control over what changes to stash, improving workflow flexibility.
5
AdvancedHandling untracked and ignored files in stash
🤔Before reading on: do you think git stash saves untracked files by default? Commit to your answer.
Concept: Git stash can include untracked and ignored files with special options.
By default, git stash ignores untracked and ignored files. Use git stash push -u to include untracked files, and git stash push -a to include all ignored files as well. Combine these with pathspec to stash specific untracked files.
Result
You can stash changes from untracked or ignored files selectively, not just tracked files.
Knowing how to stash untracked and ignored files prevents losing work on new or ignored files during stash operations.
6
ExpertLimitations and pitfalls of selective stashing
🤔Before reading on: do you think stashing specific files can cause conflicts or unexpected behavior when applying later? Commit to your answer.
Concept: Selective stashing can cause conflicts or partial restores if other changes depend on stashed files.
When you stash only some files, applying the stash later might fail if other files changed in ways that depend on the stashed files. Also, partial stashes can confuse teammates if shared. Use selective stashing carefully and test applying stashes before switching branches.
Result
You avoid surprises and conflicts by understanding the risks of partial stashes.
Recognizing the risks of selective stashing helps prevent workflow disruptions and data loss in complex projects.
Under the Hood
Git stash works by creating a commit object that saves the current state of your working directory and index (staged files). When you stash specific files, git internally creates a commit that includes only those files' changes. It uses the index and working tree to isolate changes by pathspec, then stores them in the stash stack. Applying the stash later merges these changes back into your working directory.
Why designed this way?
Git stash was designed to be a quick, temporary save area without cluttering commit history. Selective stashing was added later to give developers more control, avoiding the need to commit incomplete work. The design balances simplicity with flexibility, using existing git commit and index mechanisms to store partial snapshots.
Working Directory
┌─────────────────────────────┐
│ file1.txt (modified)        │
│ file2.txt (modified)        │
│ file3.txt (unchanged)       │
└─────────────────────────────┘
          │
          ▼
Git stash push -- file1.txt
          │
          ▼
Stash Commit (only file1.txt changes)
          │
          ▼
Working Directory after stash
┌─────────────────────────────┐
│ file2.txt (still modified)  │
│ file3.txt (unchanged)       │
└─────────────────────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does git stash save untracked files by default? Commit to yes or no before reading on.
Common Belief:Git stash saves all changes including untracked files automatically.
Tap to reveal reality
Reality:By default, git stash only saves tracked files' changes. Untracked files are ignored unless you use the -u or -a options.
Why it matters:Assuming untracked files are saved can cause loss of new files if you switch branches or reset without committing or stashing them properly.
Quick: Can you stash only some files by listing them directly? Commit to yes or no before reading on.
Common Belief:Git stash cannot stash specific files; it always stashes all changes.
Tap to reveal reality
Reality:Git stash push supports specifying file paths to stash only those files' changes.
Why it matters:Not knowing this limits your workflow flexibility and forces you to stash or commit all changes together.
Quick: Does applying a partial stash always succeed without conflicts? Commit to yes or no before reading on.
Common Belief:Applying a stash with only some files never causes conflicts.
Tap to reveal reality
Reality:Partial stashes can cause conflicts if other files depend on the stashed files or if the working directory has conflicting changes.
Why it matters:Ignoring this can lead to confusing merge conflicts and lost work during stash apply.
Quick: Does git stash save staged and unstaged changes separately by default? Commit to yes or no before reading on.
Common Belief:Git stash saves staged and unstaged changes separately by default.
Tap to reveal reality
Reality:By default, git stash saves both staged and unstaged changes together unless you use options like --staged or --keep-index.
Why it matters:Misunderstanding this can cause unexpected changes to be stashed or left behind.
Expert Zone
1
Selective stashing interacts subtly with the index state; knowing how staged vs unstaged changes are stored internally helps avoid partial stash surprises.
2
Using pathspec with stash push only stashes changes in the working directory, but untracked files require explicit -u or -a flags, which can be combined carefully.
3
Stashes are stored as commits in the refs/stash reference; understanding this allows advanced manipulation like cherry-picking stash commits or editing stash contents.
When NOT to use
Avoid selective stashing when your changes are tightly coupled across many files or when you need a clean, full snapshot. Instead, use feature branches or partial commits (git add -p) to manage work. Selective stashing is best for quick, temporary saves of isolated changes.
Production Patterns
In professional workflows, developers use selective stashing to quickly save work on one feature while fixing urgent bugs on another branch. Teams often combine selective stash with careful branch management and code reviews to keep history clean and avoid conflicts.
Connections
Feature Branching
Selective stashing complements feature branching by allowing temporary saves without committing to a branch.
Understanding selective stashing helps manage work-in-progress changes alongside feature branches, improving multitasking and context switching.
Patch Management
Selective stashing is similar to creating patches for specific files, both isolate changes for later application.
Knowing how selective stashing works deepens understanding of patch creation and application, useful in code reviews and collaboration.
Memory Paging in Operating Systems
Selective stashing is like paging out only certain memory pages to disk temporarily.
This cross-domain link shows how temporarily saving parts of a system's state is a common pattern in computing, improving resource management and multitasking.
Common Pitfalls
#1Trying to stash untracked files without using the right option.
Wrong approach:git stash push -- file1.txt
Correct approach:git stash push -u -- file1.txt
Root cause:By default, git stash ignores untracked files; forgetting -u means untracked files are not saved.
#2Assuming git stash pop removes the stash even if apply fails.
Wrong approach:git stash pop
Correct approach:git stash apply # If successful, then git stash drop
Root cause:git stash pop removes the stash only if apply succeeds; conflicts can cause loss if not careful.
#3Stashing specific files but forgetting that related files might cause conflicts later.
Wrong approach:git stash push -- file1.txt # Then switching branches without checking dependencies
Correct approach:Test stash apply on a clean branch and resolve dependencies before switching branches.
Root cause:Ignoring file dependencies leads to conflicts and broken code after applying partial stashes.
Key Takeaways
Git stash lets you save your work temporarily without committing, keeping your workspace clean.
You can stash specific files by specifying their paths with git stash push -- .
Untracked and ignored files are not stashed by default; use -u or -a options to include them.
Selective stashing requires care to avoid conflicts and partial restores that can disrupt your workflow.
Understanding how git stash works internally helps you use it effectively and avoid common mistakes.

Practice

(1/5)
1. What does the command git stash push -m "save changes" file.txt do?
easy
A. It saves changes only from file.txt to a new stash with a message.
B. It saves all changes in the working directory to a stash with a message.
C. It commits file.txt with the message "save changes".
D. It deletes file.txt and saves the rest to stash.

Solution

  1. Step 1: Understand the git stash push command

    This command saves changes in the working directory to a stash instead of committing.
  2. Step 2: Recognize the effect of specifying a file

    By adding file.txt, only changes in that file are saved to the stash, not all files.
  3. Final Answer:

    It saves changes only from file.txt to a new stash with a message. -> Option A
  4. Quick Check:

    Stash specific file = It saves changes only from file.txt to a new stash with a message. [OK]
Hint: Use git stash push with file names to stash specific files [OK]
Common Mistakes:
  • Thinking it stashes all files without specifying
  • Confusing stash with commit
  • Assuming it deletes files
2. Which of the following is the correct syntax to stash only -index.html and style.css files?
easy
A. git stash push -- -index.html style.css
B. git stash push -- files -index.html style.css
C. git stash push -index.html style.css
D. git stash push -f -index.html style.css

Solution

  1. Step 1: Recall the syntax for stashing specific files

    The correct syntax uses git stash push -- <files> to specify files.
  2. Step 2: Identify the correct option

    git stash push -- -index.html style.css uses -- before file names, which is required to separate options from file paths.
  3. Final Answer:

    git stash push -- -index.html style.css -> Option A
  4. Quick Check:

    Use -- before files to stash specific files [OK]
Hint: Always use -- before file names in stash command [OK]
Common Mistakes:
  • Omitting -- before file names
  • Using unsupported flags like -f
  • Adding extra words like 'files'
3. Given these changes:
-file1.txt modified, file2.txt modified, file3.txt unchanged.
What will be the output of git stash push -- -file1.txt followed by git stash list?
medium
A. Shows an error because multiple files are modified.
B. Shows a stash with -file1.txt and file2.txt changes saved.
C. Shows a stash with only -file1.txt changes saved.
D. Shows no stash because file2.txt is not included.

Solution

  1. Step 1: Understand what git stash push -- -file1.txt does

    This command saves only changes from -file1.txt to a new stash.
  2. Step 2: Check the stash list output

    After stashing, git stash list shows the new stash entry with only -file1.txt changes saved.
  3. Final Answer:

    Shows a stash with only -file1.txt changes saved. -> Option C
  4. Quick Check:

    Stash specific file = stash list shows that file only [OK]
Hint: Stash command saves only specified files, stash list shows saved entries [OK]
Common Mistakes:
  • Assuming all modified files are stashed
  • Expecting an error when multiple files are modified
  • Confusing stash list output with file contents
4. You run git stash push -file1.txt but get an error: error: unknown option '-file1.txt'. What is the likely cause?
medium
A. You used the wrong command; git stash save is required.
B. The file -file1.txt does not exist.
C. You need to commit changes before stashing.
D. You forgot to add -- before the file name.

Solution

  1. Step 1: Analyze the error message

    The error says unknown option '-file1.txt', meaning Git treats the file name as an option.
  2. Step 2: Identify correct syntax for stashing specific files

    You must use -- before file names to separate options from file paths.
  3. Final Answer:

    You forgot to add -- before the file name. -> Option D
  4. Quick Check:

    Missing -- causes unknown option error [OK]
Hint: Add -- before files to avoid option parsing errors [OK]
Common Mistakes:
  • Omitting -- before file names
  • Assuming file must be committed first
  • Using deprecated stash commands
5. You have modified -app.js, index.html, and style.css. You want to stash only -app.js and style.css, then later apply those changes back. Which sequence of commands correctly does this?
hard
A. git stash push -app.js style.css
git stash apply
B. git stash push -m "partial stash" -- -app.js style.css
git stash apply stash@{0}
C. git stash save -app.js style.css
git stash pop
D. git stash push -app.js style.css
git stash pop stash@{1}

Solution

  1. Step 1: Stash specific files with a message

    Use git stash push -m "partial stash" -- -app.js style.css to stash only selected files with a label.
  2. Step 2: Apply the correct stash entry

    Use git stash apply stash@{0} to apply the most recent stash explicitly.
  3. Final Answer:

    git stash push -m "partial stash" -- -app.js style.css
    git stash apply stash@{0}
    -> Option B
  4. Quick Check:

    Use -m and -- with files, then apply stash by name [OK]
Hint: Use -m for message and -- before files, then apply stash by reference [OK]
Common Mistakes:
  • Using deprecated git stash save
  • Omitting -- before file names
  • Applying stash without specifying correct stash reference