0
0
Gitdevops~15 mins

Stashing specific files in Git - Deep Dive

Choose your learning style9 modes available
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.