0
0
Gitdevops~15 mins

git stash to save changes - Deep Dive

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