0
0
Gitdevops~10 mins

Worktrees vs stashing decision in Git - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Process Flow - Worktrees vs stashing decision
Start with changes in current branch
Need to switch branch?
Yes
Are changes ready to save temporarily?
NoUse Worktree
Yes
Use Stash to save changes
Switch branch
Apply stash or continue worktree
This flow helps decide whether to use git stash or git worktree when switching branches with uncommitted changes.
Execution Sample
Git
git stash
# save changes temporarily

git checkout feature-branch
# switch branch

git stash pop
# apply saved changes
This sequence saves changes with stash, switches branch, then reapplies changes.
Process Table
StepActionState BeforeState AfterNotes
1Modify files in current branchClean working directoryUncommitted changes presentWorking directory now dirty
2Decide to switch branchUncommitted changes presentDecision pointNeed to save or isolate changes
3Use 'git stash'Uncommitted changes presentClean working directory, stash savedChanges saved in stash stack
4Switch branch with 'git checkout feature-branch'Clean working directoryOn feature-branch, clean working directoryBranch switched successfully
5Apply stash with 'git stash pop'Clean working directory, stash savedUncommitted changes reapplied, stash removedChanges restored on new branch
6Work continues on feature-branchUncommitted changes presentUncommitted changes presentReady to commit or modify further
7Alternative: Use 'git worktree add ../feature feature-branch'Uncommitted changes presentNew worktree created with feature-branch checked outChanges isolated in separate directory
8Work independently in new worktreeSeparate directory with feature-branchChanges independent from main worktreeNo stash needed, parallel work possible
9ExitWorktrees or stash used appropriatelyWorking directory state managedDecision made based on workflow needs
💡 Execution stops after changes are saved either by stash or isolated by worktree, enabling branch switch or parallel work.
Status Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5After Step 7
Working DirectoryCleanDirty (uncommitted changes)Clean (changes stashed)Clean (on new branch)Dirty (changes reapplied)Dirty (original worktree)
Current Branchmainmainmainfeature-branchfeature-branchmain
Stash StackEmptyEmpty1 stash entry1 stash entryEmpty (stash popped)Empty
Worktrees1 (main)1 (main)1 (main)1 (feature-branch)1 (feature-branch)2 (main + feature)
Key Moments - 3 Insights
Why do we need to stash changes before switching branches?
Because git won't let you switch branches if you have uncommitted changes that conflict. Stashing saves them temporarily (see execution_table step 3).
When is using a worktree better than stashing?
When you want to work on multiple branches at the same time without switching or losing changes. Worktrees create separate folders for each branch (see execution_table step 7).
What happens to the stash after 'git stash pop'?
The stash is applied to the working directory and then removed from the stash stack (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the working directory after step 3?
ADirty, changes still present
BClean, no changes saved
CClean, changes saved in stash
DDirty, changes lost
💡 Hint
Check the 'State After' column for step 3 in the execution_table.
At which step does the branch actually switch to 'feature-branch'?
AStep 2
BStep 4
CStep 5
DStep 7
💡 Hint
Look for the action 'git checkout feature-branch' in the execution_table.
If you want to work on two branches simultaneously without switching, which step shows the best approach?
AStep 7 (create worktree)
BStep 5 (apply stash)
CStep 3 (stash changes)
DStep 4 (switch branch)
💡 Hint
Refer to the notes about worktrees in the execution_table at step 7.
Concept Snapshot
Git stash saves uncommitted changes temporarily to switch branches safely.
Git worktree creates separate folders to work on multiple branches simultaneously.
Use stash for quick save and switch.
Use worktree for parallel branch work.
Stash must be popped to restore changes.
Worktrees keep changes isolated without switching.
Full Transcript
This visual execution compares git stash and git worktree for managing uncommitted changes when switching branches. First, if you have changes and want to switch branches, git requires a clean working directory. You can save changes temporarily using 'git stash', which cleans your directory and stores changes safely. Then you switch branches with 'git checkout' and reapply changes with 'git stash pop'. Alternatively, you can create a new worktree with 'git worktree add' to have a separate folder checked out to another branch. This allows working on multiple branches at once without switching or stashing. The execution table shows each step's state changes, including working directory status, current branch, stash stack, and worktrees. Key moments clarify why stashing is needed before switching, when worktrees are better, and what happens to stash after popping. The quiz tests understanding of working directory states, branch switching steps, and when to use worktrees. The snapshot summarizes the decision: stash for temporary save and switch, worktree for parallel work.