What if you could pause your work instantly and never lose a single change?
Why git stash to save changes? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are working on some code changes but suddenly need to switch to a different task or branch. You want to keep your current work safe without committing unfinished code.
Manually copying files or creating temporary commits is slow and messy. It's easy to forget what you changed or accidentally lose work. Switching branches without saving can cause conflicts or lost changes.
git stash lets you quickly save your current changes aside without committing. You can switch tasks or branches safely, then come back and restore your work exactly as you left it.
cp file.txt file_backup.txt # switch branch # copy back changes manually
git stash # switch branch # git stash pop
You can pause your work anytime, switch contexts safely, and resume without losing or mixing up changes.
You're fixing a bug but get an urgent request to review a teammate's code on another branch. Using git stash, you save your bug fix progress, switch branches to review, then return and continue seamlessly.
Manual saving of changes is slow and risky.
git stash saves your work quickly without commits.
It helps you switch tasks safely and resume work easily.
Practice
git stash command do?Solution
Step 1: Understand the purpose of
The command saves your current uncommitted changes temporarily without committing them.git stashStep 2: Compare with other git commands
Unlike commit, stash does not save changes permanently; it allows switching tasks without losing work.Final Answer:
Temporarily saves your uncommitted changes to switch tasks -> Option CQuick Check:
git stash = temporary save [OK]
- Thinking stash commits changes permanently
- Confusing stash with branch creation
- Assuming stash deletes files
Solution
Step 1: Identify the modern git stash command
The recommended command to save changes isgit stash push, which explicitly pushes changes to the stash.Step 2: Eliminate incorrect options
git stash saveis deprecated,git stash commitandgit stash addare invalid commands.Final Answer:
git stash push -> Option DQuick Check:
Use git stash push to save changes [OK]
- Using deprecated 'git stash save'
- Trying 'git stash commit' which doesn't exist
- Confusing stash with add or commit commands
git stash push -m "work in progress" git stash list git stash apply
What will be the output of
git stash list?Solution
Step 1: Understand the effect of
This command saves current changes with the message "work in progress" as the latest stash entry.git stash push -m "work in progress"Step 2: Check
Since this is the first stash, it appears asgit stash listoutputstash@{0}: On main: work in progress.Final Answer:
stash@{0}: On main: work in progress -> Option BQuick Check:
First stash is stash@{0} with message [OK]
- Expecting no stash entries after push
- Confusing stash@{0} with stash@{1}
- Assuming apply removes stash entry
git stash push but accidentally included untracked files. Which command fixes this by stashing only tracked files?Solution
Step 1: Understand the problem with untracked files
By default,git stash pushdoes not stash untracked files unless specified.Step 2: Identify the correct option to stash only tracked files
--keep-indexstashes changes but keeps the index intact, effectively ignoring untracked files.Final Answer:
git stash push --keep-index -> Option AQuick Check:
Use --keep-index to stash only tracked files [OK]
- 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
stash@{0}: On main: fix bug
stash@{1}: On main: add featureYou want to apply the older stash (add feature) but keep both stashes after applying. Which command should you use?
Solution
Step 1: Understand difference between apply and pop
git stash applyapplies the stash but keeps it saved;git stash popapplies and removes it.Step 2: Choose command to apply older stash without removing it
Usegit stash apply stash@{1}to apply the older stash and keep both stashes intact.Final Answer:
git stash apply stash@{1} -> Option AQuick Check:
Apply keeps stash, pop removes stash [OK]
- Using pop removes stash entry
- Dropping stash deletes it without applying
- Confusing branch command with apply
