Bird
Raised Fist0
Gitdevops~5 mins

Why stashing saves work temporarily in Git - Why It Works

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
Introduction
Sometimes you start working on changes but need to switch tasks quickly without losing your progress. Git stash lets you save your unfinished work safely and come back to it later.
When you are halfway through editing files but need to switch to a different branch to fix a bug
When you want to pull updates from the remote repository but have uncommitted changes
When you want to test something quickly without committing your current work
When you want to clean your working directory temporarily without losing your changes
When you want to share your current work with a teammate but it is not ready to commit
Commands
This command saves your current uncommitted changes and cleans your working directory so you can switch tasks safely.
Terminal
git stash
Expected OutputExpected
Saved working directory and index state WIP on main: abc1234 Initial commit
Shows the list of all saved stashes so you can see what work you saved temporarily.
Terminal
git stash list
Expected OutputExpected
stash@{0}: WIP on main: abc1234 Initial commit
Restores the most recent saved changes back to your working directory so you can continue working.
Terminal
git stash apply
Expected OutputExpected
On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: example.txt no changes added to commit (use "git add" and/or "git commit" to update your working tree)
Deletes the most recent stash after you no longer need it to keep your stash list clean.
Terminal
git stash drop
Expected OutputExpected
Dropped refs/stash@{0} (abc1234 Initial commit)
Key Concept

If you remember nothing else, remember: git stash temporarily saves your uncommitted work so you can switch tasks without losing progress.

Common Mistakes
Running git stash but forgetting to apply it later
Your saved changes stay hidden and you might lose track of your work.
Always run git stash apply or git stash pop to restore your changes when ready.
Using git stash without committing important changes first
Stash is temporary and can be lost if not managed carefully.
Commit important work regularly and use stash only for temporary saves.
Dropping stash without checking its contents
You might delete work you still need.
Use git stash list and git stash show before dropping to confirm.
Summary
git stash saves your uncommitted changes temporarily and cleans your working directory.
git stash list shows all saved stashes so you can track your temporary work.
git stash apply restores saved changes so you can continue working where you left off.

Practice

(1/5)
1. What is the main purpose of git stash in Git?
easy
A. To merge two branches automatically
B. To permanently delete untracked files
C. To save your current changes temporarily without committing
D. To create a new branch from the current one

Solution

  1. Step 1: Understand what git stash does

    git stash saves your current working changes temporarily without committing them to the branch.
  2. Step 2: Compare with other options

    The other options describe different Git commands or actions unrelated to stashing.
  3. Final Answer:

    To save your current changes temporarily without committing -> Option C
  4. Quick Check:

    Stashing = Temporary save without commit [OK]
Hint: Stash = save work now, come back later [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 temporarily in Git?
easy
A. git save
B. git commit -m 'temp'
C. git push stash
D. git stash

Solution

  1. Step 1: Recall the correct syntax for stashing

    The correct command to save changes temporarily is git stash.
  2. Step 2: Check other options for correctness

    git save and git push stash are invalid commands. git commit -m 'temp' commits changes permanently, not temporarily.
  3. Final Answer:

    git stash -> Option D
  4. Quick Check:

    Temporary save command = git stash [OK]
Hint: Remember: stash means temporary save [OK]
Common Mistakes:
  • Using git save instead of git stash
  • Confusing commit with stash
  • Trying to push stash as a branch
3. Given the following commands run in order:
git stash
git checkout main
git stash pop

What happens after git stash pop?
medium
A. Your saved changes are restored and removed from stash
B. Your changes are permanently deleted
C. You switch back to the previous branch automatically
D. A new stash is created with the same changes

Solution

  1. Step 1: Understand the commands sequence

    git stash saves changes temporarily, git checkout main switches branch, and git stash pop restores saved changes and removes them from stash.
  2. Step 2: Analyze the effect of git stash pop

    This command applies the saved changes back to the working directory and deletes the stash entry.
  3. Final Answer:

    Your saved changes are restored and removed from stash -> Option A
  4. Quick Check:

    Stash pop = restore + remove stash [OK]
Hint: Pop restores stash and deletes it [OK]
Common Mistakes:
  • Thinking stash pop deletes changes permanently
  • Assuming branch switches back automatically
  • Believing stash pop creates a new stash
4. You ran git stash but later realize your changes are missing after switching branches. What is the most likely mistake?
medium
A. You forgot to run git stash pop to restore changes
B. You committed the changes instead of stashing
C. You deleted the stash manually before switching branches
D. You ran git stash apply instead of git stash

Solution

  1. Step 1: Understand what git stash does

    git stash saves changes temporarily but does not restore them automatically.
  2. Step 2: Identify why changes are missing

    If you switch branches without restoring stash using git stash pop or git stash apply, changes stay hidden in stash.
  3. Final Answer:

    You forgot to run git stash pop to restore changes -> Option A
  4. Quick Check:

    Stash saves but does not restore automatically [OK]
Hint: Stash saves; pop or apply restores [OK]
Common Mistakes:
  • Assuming stash auto-restores on branch switch
  • Confusing stash with commit
  • Deleting stash accidentally
5. You have uncommitted changes in your current branch but need to switch to another branch to fix a bug. Which sequence of commands correctly saves your work temporarily and restores it after fixing the bug?
hard
A. git checkout bugfix; fix bug; git stash; git checkout main; git stash apply
B. git stash; git checkout bugfix; fix bug; git checkout main; git stash pop
C. git stash pop; git checkout bugfix; fix bug; git stash
D. git commit -m 'temp'; git checkout bugfix; fix bug; git checkout main; git reset --soft HEAD~1

Solution

  1. Step 1: Save current changes before switching branches

    Use git stash to save uncommitted changes temporarily.
  2. Step 2: Switch to bugfix branch, fix the bug, then return and restore changes

    After fixing, switch back to main branch and run git stash pop to restore saved changes.
  3. Final Answer:

    git stash; git checkout bugfix; fix bug; git checkout main; git stash pop -> Option B
  4. Quick Check:

    Stash before switch, pop after return [OK]
Hint: Stash before switch, pop after return [OK]
Common Mistakes:
  • Committing temporary changes unnecessarily
  • Running stash pop before stashing
  • Applying stash on wrong branch