Bird
Raised Fist0
Gitdevops~15 mins

git stash to save changes - 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 - git stash to save changes
What is it?
Git stash is a command that temporarily saves your unfinished changes in a special storage area. It allows you to clean your working directory without committing incomplete work. Later, you can restore these changes and continue working where you left off. This helps keep your project clean and organized.
Why it matters
Without git stash, you might have to commit half-done work or lose your changes when switching tasks. This can clutter your project history or cause lost work. Git stash solves this by letting you pause your work safely and switch contexts quickly, improving productivity and reducing mistakes.
Where it fits
Before learning git stash, you should understand basic git commands like git add, git commit, and git checkout. After mastering stash, you can explore advanced git workflows like branching strategies, rebasing, and resolving merge conflicts.
Mental Model
Core Idea
Git stash acts like a temporary shelf where you can put your current work aside to focus on something else, then pick it back up later exactly as you left it.
Think of it like...
Imagine you are cooking and suddenly need to answer the door. You quickly put your chopping board and knife aside on a clean shelf to keep them safe and clean. After answering, you take them back and continue chopping without losing your progress.
Working Directory
  │
  ├─ Uncommitted Changes
  │      ↓ git stash
  ├─ Stash Storage (temporary shelf)
  │      ↓ git stash apply/pop
  └─ Restored Changes
Build-Up - 7 Steps
1
FoundationUnderstanding Uncommitted Changes
🤔
Concept: Learn what uncommitted changes are and why they matter.
When you edit files in a git project, these edits are called uncommitted changes. They are not saved in the project history until you commit them. These changes can be new files, edits, or deletions.
Result
You can see your uncommitted changes using 'git status'.
Knowing what uncommitted changes are helps you understand why you might want to save them temporarily without committing.
2
FoundationWhat Does Git Stash Do?
🤔
Concept: Git stash saves uncommitted changes temporarily and cleans your working directory.
Running 'git stash' saves your current uncommitted changes into a hidden storage and resets your working directory to the last commit state. Your changes are safe but hidden.
Result
Your working directory becomes clean, ready for other tasks.
Understanding stash as a temporary save spot helps you manage work interruptions without losing progress.
3
IntermediateSaving and Listing Stashes
🤔Before reading on: do you think git stash saves only tracked files or also new untracked files? Commit to your answer.
Concept: Learn how to save changes and see all saved stashes.
Use 'git stash' to save tracked changes. To include untracked files, use 'git stash -u'. List saved stashes with 'git stash list'. Each stash has a name like 'stash@{0}'.
Result
You can save different sets of changes and see them listed for later use.
Knowing how to save and list stashes lets you manage multiple work pauses effectively.
4
IntermediateRestoring Changes from Stash
🤔Before reading on: does 'git stash apply' remove the stash after restoring or keep it? Commit to your answer.
Concept: Learn how to bring back stashed changes to your working directory.
Use 'git stash apply' to restore changes but keep the stash saved. Use 'git stash pop' to restore and remove the stash. You can specify which stash to apply by name.
Result
Your saved changes return to your working directory, ready to continue.
Understanding the difference between apply and pop helps avoid losing stashes unintentionally.
5
IntermediateStashing Parts of Changes
🤔
Concept: You can stash only specific files or parts of your changes.
Use 'git stash push ' to stash changes from specific files only. This helps when you want to save some work but keep other changes in place.
Result
Only selected changes are saved in the stash, others remain in your working directory.
Knowing partial stashing gives you fine control over what to pause and what to keep working on.
6
AdvancedHandling Conflicts When Applying Stash
🤔Before reading on: do you think stash apply always merges cleanly or can cause conflicts? Commit to your answer.
Concept: Sometimes restoring stashed changes causes conflicts if the code changed meanwhile.
If your working directory changed since stashing, applying stash may cause conflicts. Git will mark conflicts in files for you to resolve manually.
Result
You must fix conflicts before continuing work.
Knowing stash can cause conflicts prepares you to handle real-world interruptions safely.
7
ExpertUsing Stash in Complex Workflows
🤔Before reading on: can you stash changes across branches or only within one branch? Commit to your answer.
Concept: Git stash works across branches and can be combined with other git commands for advanced workflows.
You can stash changes on one branch, switch to another branch, and apply the stash there. This helps when you need to quickly switch tasks. Also, you can create named stashes for clarity and use 'git stash branch' to create a new branch from a stash.
Result
You can pause work anywhere and resume flexibly, improving multitasking.
Understanding stash's flexibility across branches unlocks powerful git workflows for real projects.
Under the Hood
Git stash works by creating a commit object that stores the differences between your working directory and the last commit. It saves this commit in a hidden area called the stash stack. When you apply or pop a stash, git merges these changes back into your working directory. This process uses git's internal commit and merge mechanisms.
Why designed this way?
Git stash was designed to leverage git's existing commit and merge system to avoid reinventing storage. This makes it efficient and reliable. Using a stack structure allows multiple stashes to be saved and retrieved in order. Alternatives like temporary branches were more cumbersome and cluttered history.
Working Directory Changes
      │
      ▼
  ┌───────────────┐
  │ git stash save│
  └───────────────┘
      │
      ▼
  ┌───────────────┐
  │ Stash Stack   │
  │ (commit diff) │
  └───────────────┘
      │
      ▼
  ┌───────────────┐
  │ git stash apply│
  └───────────────┘
      │
      ▼
  Working Directory Restored
Myth Busters - 4 Common Misconceptions
Quick: Does 'git stash' save untracked files by default? Commit yes or no.
Common Belief:Git stash saves all changes including new untracked files by default.
Tap to reveal reality
Reality:By default, git stash only saves tracked files' changes. Untracked files are ignored unless you use 'git stash -u'.
Why it matters:Assuming untracked files are saved can cause loss of new files if you switch branches or reset without saving them explicitly.
Quick: Does 'git stash apply' remove the stash after restoring? Commit yes or no.
Common Belief:'git stash apply' removes the stash from the stash list after applying it.
Tap to reveal reality
Reality:'git stash apply' restores changes but keeps the stash saved. Only 'git stash pop' removes it.
Why it matters:Confusing these commands can lead to accidentally losing stashed work or cluttering stash list.
Quick: Can you stash changes across different branches? Commit yes or no.
Common Belief:You can only apply a stash on the same branch where it was created.
Tap to reveal reality
Reality:Stashes are independent of branches and can be applied on any branch, though conflicts may occur.
Why it matters:Knowing this allows flexible workflows and prevents unnecessary branch switching.
Quick: Does git stash save staged changes separately from unstaged? Commit yes or no.
Common Belief:Git stash only saves unstaged changes, staged changes remain untouched.
Tap to reveal reality
Reality:Git stash saves both staged and unstaged changes by default.
Why it matters:Misunderstanding this can cause confusion about what changes are saved and restored.
Expert Zone
1
Stashes are stored as commits but hidden from normal git logs, so they don't clutter history but can be inspected with git commands.
2
You can create named stashes with 'git stash push -m "message"' to keep track of multiple stashes clearly.
3
Using 'git stash branch ' creates a new branch from a stash, combining stash and branch creation in one step.
When NOT to use
Avoid using git stash for long-term storage or sharing work; use branches or commits instead. Also, do not rely on stash for large binary files or untracked files without explicit flags.
Production Patterns
In professional workflows, developers stash changes to quickly switch tasks or pull updates without committing incomplete work. Teams use named stashes and stash branches to organize work-in-progress. CI/CD pipelines rarely use stash but rely on clean commits.
Connections
Version Control Branching
Git stash complements branching by allowing temporary saving of work before switching branches.
Understanding stash helps manage context switches in branching workflows without cluttering commit history.
Task Switching in Productivity
Git stash models the concept of pausing and resuming tasks cleanly, similar to how people put aside work to focus on urgent tasks.
Recognizing stash as a task switch tool improves mental organization and reduces stress in coding.
Undo/Redo Mechanisms in Software
Git stash acts like an undo stack for uncommitted changes, allowing temporary rollback and restoration.
Knowing stash parallels undo systems clarifies its role as a reversible save point.
Common Pitfalls
#1Losing untracked files by stashing without including them.
Wrong approach:git stash # untracked files are not saved
Correct approach:git stash -u # includes untracked files in stash
Root cause:Assuming 'git stash' saves all files by default, ignoring untracked files.
#2Applying stash and expecting it to be removed automatically.
Wrong approach:git stash apply stash@{0} # stash remains saved
Correct approach:git stash pop stash@{0} # stash is removed after applying
Root cause:Confusing 'apply' with 'pop' commands and their effects on stash list.
#3Stashing changes and then switching branches without applying stash, causing confusion about where changes are.
Wrong approach:git stash git checkout other-branch # forget to apply stash
Correct approach:git stash git checkout other-branch git stash apply
Root cause:Not understanding stash is independent of branches and must be applied explicitly.
Key Takeaways
Git stash is a temporary storage for uncommitted changes, allowing you to pause work without committing.
By default, stash saves tracked changes; use flags to include untracked files.
You can apply or pop stashes to restore changes, with pop removing the stash after use.
Stash works across branches, enabling flexible task switching in git workflows.
Understanding stash prevents lost work and cluttered commit history during multitasking.

Practice

(1/5)
1. What does the git stash command do?
easy
A. Commits your changes permanently to the repository
B. Deletes all your untracked files
C. Temporarily saves your uncommitted changes to switch tasks
D. Creates a new branch from the current state

Solution

  1. Step 1: Understand the purpose of git stash

    The command saves your current uncommitted changes temporarily without committing them.
  2. Step 2: Compare with other git commands

    Unlike commit, stash does not save changes permanently; it allows switching tasks without losing work.
  3. Final Answer:

    Temporarily saves your uncommitted changes to switch tasks -> Option C
  4. Quick Check:

    git stash = temporary save [OK]
Hint: Stash saves changes temporarily without committing [OK]
Common Mistakes:
  • Thinking stash commits changes permanently
  • Confusing stash with branch creation
  • Assuming stash deletes files
2. Which of the following is the correct command to save your current changes using git stash?
easy
A. git stash add
B. git stash save
C. git stash commit
D. git stash push

Solution

  1. Step 1: Identify the modern git stash command

    The recommended command to save changes is git stash push, which explicitly pushes changes to the stash.
  2. Step 2: Eliminate incorrect options

    git stash save is deprecated, git stash commit and git stash add are invalid commands.
  3. Final Answer:

    git stash push -> Option D
  4. Quick Check:

    Use git stash push to save changes [OK]
Hint: Use 'git stash push' to save changes safely [OK]
Common Mistakes:
  • Using deprecated 'git stash save'
  • Trying 'git stash commit' which doesn't exist
  • Confusing stash with add or commit commands
3. Given the following commands run in sequence:
git stash push -m "work in progress"
git stash list
git stash apply

What will be the output of git stash list?
medium
A. No stash entries found.
B. stash@{0}: On main: work in progress
C. Error: stash not found
D. stash@{1}: On main: work in progress

Solution

  1. Step 1: Understand the effect of git stash push -m "work in progress"

    This command saves current changes with the message "work in progress" as the latest stash entry.
  2. Step 2: Check git stash list output

    Since this is the first stash, it appears as stash@{0}: On main: work in progress.
  3. Final Answer:

    stash@{0}: On main: work in progress -> Option B
  4. Quick Check:

    First stash is stash@{0} with message [OK]
Hint: First stash is always stash@{0} in the list [OK]
Common Mistakes:
  • Expecting no stash entries after push
  • Confusing stash@{0} with stash@{1}
  • Assuming apply removes stash entry
4. You ran git stash push but accidentally included untracked files. Which command fixes this by stashing only tracked files?
medium
A. git stash push --keep-index
B. git stash push --only-tracked
C. git stash push --no-untracked
D. git stash push --include-untracked

Solution

  1. Step 1: Understand the problem with untracked files

    By default, git stash push does not stash untracked files unless specified.
  2. Step 2: Identify the correct option to stash only tracked files

    --keep-index stashes changes but keeps the index intact, effectively ignoring untracked files.
  3. Final Answer:

    git stash push --keep-index -> Option A
  4. Quick Check:

    Use --keep-index to stash only tracked files [OK]
Hint: Use --keep-index to exclude untracked files from stash [OK]
Common Mistakes:
  • Using --include-untracked adds untracked files instead of excluding
  • Assuming --no-untracked or --only-tracked are valid options
  • Confusing stash options with git add options
5. You have two stashes saved:
stash@{0}: On main: fix bug
stash@{1}: On main: add feature

You want to apply the older stash (add feature) but keep both stashes after applying. Which command should you use?
hard
A. git stash apply stash@{1}
B. git stash pop stash@{1}
C. git stash drop stash@{1}
D. git stash branch stash@{1}

Solution

  1. Step 1: Understand difference between apply and pop

    git stash apply applies the stash but keeps it saved; git stash pop applies and removes it.
  2. Step 2: Choose command to apply older stash without removing it

    Use git stash apply stash@{1} to apply the older stash and keep both stashes intact.
  3. Final Answer:

    git stash apply stash@{1} -> Option A
  4. Quick Check:

    Apply keeps stash, pop removes stash [OK]
Hint: Use 'git stash apply' to keep stash after applying [OK]
Common Mistakes:
  • Using pop removes stash entry
  • Dropping stash deletes it without applying
  • Confusing branch command with apply