Bird
Raised Fist0
Gitdevops~20 mins

git stash to save changes - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Git Stash Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of git stash list after stashing changes?
You have modified files in your git repository. You run git stash to save your changes temporarily. What will git stash list show immediately after?
Git
git stash

git stash list
Astash@{0}: WIP on main: <commit-hash> Your last commit message
BNo stash entries found.
Cfatal: No stash found.
DError: stash command not recognized.
Attempts:
2 left
💡 Hint
Think about what git stash does and how git stash list displays saved stashes.
🧠 Conceptual
intermediate
1:30remaining
What happens to your working directory after running git stash?
You have uncommitted changes in your working directory. You run git stash. What is the state of your working directory immediately after?
AWorking directory is partially cleaned; only staged changes are saved.
BChanges remain in the working directory but are also saved in stash.
CChanges are deleted permanently from the working directory and stash.
DAll changes are saved and the working directory is clean, matching the last commit.
Attempts:
2 left
💡 Hint
Consider what stash means: saving changes temporarily and cleaning your workspace.
🔀 Workflow
advanced
2:00remaining
Which sequence of commands correctly saves changes and then applies them back?
You want to save your current changes temporarily and later bring them back. Which command sequence does this correctly?
A
git stash create
...
git stash drop
B
git stash
...
git stash apply
C
git stash save
...
git stash pop
D
git stash store
...
git stash branch
Attempts:
2 left
💡 Hint
Remember the basic commands to save and reapply stashed changes.
Troubleshoot
advanced
1:30remaining
What error occurs if you run git stash apply with no stashes saved?
You run git stash apply but you never saved any stash before. What error message will git show?
Git
git stash apply
ANo stash found.
Bfatal: You do not have the initial commit yet
Cfatal: bad revision 'stash@{0}'
Derror: Your local changes to the following files would be overwritten by merge
Attempts:
2 left
💡 Hint
Think about what happens when git tries to apply a stash that does not exist.
Best Practice
expert
2:00remaining
Which command safely saves changes including untracked files?
You want to stash your changes but also include new untracked files. Which command should you use?
Agit stash push -u
Bgit stash save --include-untracked
Cgit stash apply --all
Dgit stash store --untracked
Attempts:
2 left
💡 Hint
Look for the modern command to stash including untracked files.

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