0
0
Gitdevops~20 mins

Worktrees vs stashing decision in Git - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Worktrees vs Stashing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use git worktree instead of git stash?

You have uncommitted changes and want to switch to a different branch to test something without losing your current work. Which situation best fits using git worktree instead of git stash?

AYou want to temporarily save changes and clean your working directory to switch branches quickly.
BYou want to discard all current changes and start fresh on a new branch.
CYou want to merge your current changes into the other branch without switching.
DYou want to keep your current changes visible and editable while working on another branch simultaneously.
Attempts:
2 left
💡 Hint

Think about whether you want to work on two branches at the same time with your changes visible.

💻 Command Output
intermediate
1:30remaining
Output of git stash list after stashing changes

You run the following commands in a git repository:

echo 'change' >> file.txt
git stash

What will git stash list show immediately after?

Git
git stash list
Aerror: no stash found.
BNo stash entries found.
Cstash@{0}: WIP on main: <commit-hash> <commit-message>
Dstash@{1}: WIP on main: <commit-hash> <commit-message>
Attempts:
2 left
💡 Hint

Think about what stash numbering starts at and what happens after the first stash.

🔀 Workflow
advanced
2:30remaining
Correct sequence to create and use a git worktree

You want to create a new worktree for branch feature in a directory ../feature-worktree. Which sequence of commands correctly achieves this?

A1,2,4
B2,1,3
C1,3,2
D4,1,2
Attempts:
2 left
💡 Hint

Remember that git worktree add creates and checks out the branch automatically.

Troubleshoot
advanced
2:00remaining
Error when switching branches with uncommitted changes

You try to switch branches with git checkout feature but get this error:

error: Your local changes to the following files would be overwritten by checkout:

What is the best way to handle this if you want to keep your changes but also switch branches?

AUse <code>git reset --hard</code> to discard changes, then switch branches.
BUse <code>git worktree add</code> to create a new worktree for the other branch.
CUse <code>git commit -a -m 'temp'</code> to commit changes, then switch branches.
DUse <code>git stash</code> to save changes, switch branches, then <code>git stash pop</code> to reapply.
Attempts:
2 left
💡 Hint

Consider how to keep your current changes visible and work on another branch simultaneously.

Best Practice
expert
3:00remaining
Choosing between git stash and git worktree for parallel feature development

You are developing two features in parallel. You want to switch between them frequently without losing work or creating many commits. Which approach is best?

AUse <code>git worktree</code> to have separate working directories for each feature branch.
BUse <code>git reset --hard</code> to discard changes before switching branches.
CCommit partial work on one branch before switching to the other branch.
DUse <code>git stash</code> to save and restore changes each time you switch branches.
Attempts:
2 left
💡 Hint

Think about how to keep work visible and avoid repeated stashing or committing.