What if you could switch tasks instantly without losing your unfinished work or creating a mess?
Worktrees vs stashing decision in Git - When to Use Which
Imagine you are working on a project and suddenly need to switch to a different task. You have some unfinished changes, but you want to quickly check or work on another branch without losing your current work.
Manually saving changes by copying files or creating patches is slow and risky. You might forget to save something or accidentally overwrite your work. Switching branches without saving causes conflicts or lost changes, making you anxious and wasting time.
Using worktrees or stashing in Git lets you handle this smoothly. Worktrees let you have multiple working folders for different branches at once. Stashing temporarily saves your changes so you can switch branches safely and come back later.
cp -r project project_backup # switch branch rm -rf project git checkout other-branch # restore changes manually
git stash # switch branch git checkout other-branch # later git stash pop
You can switch tasks instantly without losing work or creating confusion, making your workflow faster and less stressful.
A developer is fixing a bug on one branch but gets a request to review code on another branch. Using worktrees, they open the second branch in a new folder and review without disturbing their bug fix.
Manual saving and switching is slow and error-prone.
Worktrees allow multiple branches checked out simultaneously in separate folders.
Stashing saves changes temporarily to switch branches safely.
Practice
git stash in Git?Solution
Step 1: Understand what
git stashdoesgit stashsaves your current changes temporarily and cleans your working directory so you can switch branches or work on something else without committing.Step 2: Compare with other options
Creating a new branch in a separate folder is done with worktrees, not stash. Merging branches and deleting untracked files are unrelated commands.Final Answer:
Temporarily save changes and clean the working directory -> Option BQuick Check:
Stash = Temporary save changes [OK]
- Confusing stash with branch creation
- Thinking stash deletes files permanently
- Mixing stash with merge operations
feature in folder feature-folder?Solution
Step 1: Recall the syntax for adding a worktree
The correct syntax isgit worktree add <path> <branch>. Sogit worktree add feature-folder featurecreates a new folder with the branch checked out.Step 2: Check other options for correctness
git stash createis invalid for this purpose.git branch addis not a valid command.git checkout -bcreates a branch but does not create a separate folder.Final Answer:
git worktree add feature-folder feature -> Option AQuick Check:
Worktree add = git worktree add [OK]
- Using stash commands to create worktrees
- Confusing branch creation with worktree creation
- Incorrect order of arguments in commands
main. You want to switch to feature branch without losing changes. What happens if you run git stash then git checkout feature?Solution
Step 1: Understand what
git stashdoes before switchinggit stashsaves your uncommitted changes safely and cleans your working directory so you can switch branches without conflicts.Step 2: Effect of
Since the working directory is clean after stash, Git switches togit checkout featureafter stashfeaturebranch without errors or losing changes. You can later apply the stash.Final Answer:
Changes are saved temporarily, branch switches cleanly -> Option CQuick Check:
Stash saves changes, checkout switches branch [OK]
- Assuming changes are lost after stash
- Expecting automatic merge of stashed changes
- Thinking Git errors on switching with stash
git worktree add feature but got an error. What is the most likely cause?Solution
Step 1: Check the command syntax
git worktree addrequires two arguments: folder path and branch name. The commandgit worktree add featureonly has one argument, so it is missing the folder name.Step 2: Understand error cause
Missing folder argument causes Git to throw a syntax error. Uncommitted changes or branch existence issues cause different errors, but syntax error is most likely here.Final Answer:
The folder name is missing, causing a syntax error -> Option AQuick Check:
Worktree add needs folder and branch [OK]
- Ignoring missing folder argument
- Assuming uncommitted changes block worktree add
- Confusing stash necessity with worktree creation
main and feature, simultaneously without committing unfinished work on main. Which approach is best?Solution
Step 1: Understand the need for parallel work
You want to work on two branches at the same time without committing unfinished work onmain. Stashing and switching repeatedly is slow and error-prone.Step 2: Choose the best tool for parallel branch work
Worktrees allow you to have multiple branches checked out in different folders simultaneously. This lets you work onmainandfeatureside by side without switching or stashing.Final Answer:
Create a worktree forfeaturebranch to work in a separate folder -> Option DQuick Check:
Worktrees = parallel branches in folders [OK]
- Using stash repeatedly instead of worktrees
- Committing unfinished work prematurely
- Deleting needed branches accidentally
Ready to prove it?
Pass the test and this pattern moves unknown
