12 minworkshop

Parallel Work Workshop

Parallel Work Workshop

Practice using worktrees and stashing to manage multiple tasks simultaneously.

Setup

Create a repository to practice with:

bash
1$ mkdir parallel-practice && cd parallel-practice
2$ git init
3
4$ echo "# Project" > README.md
5$ git add README.md
6$ git commit -m "initial commit"
7
8$ echo "console.log('app');" > app.js
9$ git add app.js
10$ git commit -m "add app.js"

Part 1: Worktrees

Exercise 1: Create a Hotfix Worktree

Imagine you're working on a feature when an urgent bug report comes in.

bash
1# Create a branch for the hotfix
2$ git branch hotfix/urgent-fix
3
4# Create a worktree for it
5$ git worktree add ../parallel-practice-hotfix hotfix/urgent-fix
6Preparing worktree (checking out 'hotfix/urgent-fix')
7HEAD is now at abc1234 add app.js

Exercise 2: List Your Worktrees

bash
1$ git worktree list
2/path/to/parallel-practice abc1234 [main]
3/path/to/parallel-practice-hotfix abc1234 [hotfix/urgent-fix]

Exercise 3: Work in the Hotfix Worktree

bash
1$ cd ../parallel-practice-hotfix
2
3# Make the hotfix
4$ echo "console.log('fixed!');" >> app.js
5$ git add app.js
6$ git commit -m "fix: urgent bug"
7
8# Verify
9$ git log --oneline -2
10def5678 fix: urgent bug
11abc1234 add app.js

Exercise 4: Return and Remove Worktree

bash
1# Go back to main project
2$ cd ../parallel-practice
3
4# Remove the hotfix worktree
5$ git worktree remove ../parallel-practice-hotfix
6
7# Verify it's gone
8$ git worktree list
9/path/to/parallel-practice abc1234 [main]

Exercise 5: Create Worktree with New Branch

bash
1# Create worktree with a new branch in one command
2$ git worktree add -b feature/new-thing ../parallel-practice-feature main
3Preparing worktree (new branch 'feature/new-thing')
4HEAD is now at abc1234 add app.js
5
6# Verify
7$ git worktree list
8/path/to/parallel-practice abc1234 [main]
9/path/to/parallel-practice-feature abc1234 [feature/new-thing]

Clean up:

bash
1$ git worktree remove ../parallel-practice-feature

Part 2: Stashing

Exercise 6: Basic Stash

Make some uncommitted changes:

bash
1$ echo "work in progress" >> app.js
2$ echo "new file" > wip.txt
3
4$ git status
5Changes not staged for commit:
6 modified: app.js
7
8Untracked files:
9 wip.txt

Stash them (note: untracked files need -u):

bash
1$ git stash push -u -m "WIP: feature work"
2Saved working directory and index state On main: WIP: feature work
3
4$ git status
5On branch main
6nothing to commit, working tree clean

Exercise 7: List and Show Stashes

bash
1$ git stash list
2stash@{0}: On main: WIP: feature work
3
4$ git stash show
5 app.js | 1 +
6 wip.txt | 1 +
7 2 files changed, 2 insertions(+)
8
9$ git stash show -p

Exercise 8: Do Other Work While Stashed

bash
1# Simulate urgent work
2$ echo "urgent change" > urgent.txt
3$ git add urgent.txt
4$ git commit -m "feat: urgent feature"
5
6$ git log --oneline -2
7ghi9012 feat: urgent feature
8abc1234 add app.js

Exercise 9: Restore Stashed Work

bash
1$ git stash pop
2On branch main
3Changes not staged for commit:
4 modified: app.js
5
6Untracked files:
7 wip.txt
8
9Dropped refs/stash@{0} (xyz...)
10
11$ cat app.js
12console.log('app');
13work in progress

Your work is restored!

Exercise 10: Multiple Stashes

Create several stashes:

bash
1# First stash
2$ echo "change 1" >> app.js
3$ git stash push -m "change 1"
4
5# Second stash
6$ echo "change 2" >> app.js
7$ git stash push -m "change 2"
8
9# Third stash
10$ echo "change 3" >> app.js
11$ git stash push -m "change 3"
12
13$ git stash list
14stash@{0}: On main: change 3
15stash@{1}: On main: change 2
16stash@{2}: On main: change 1

Apply a specific stash:

bash
1$ git stash apply stash@{1}
2
3$ cat app.js
4console.log('app');
5work in progress
6change 2

Exercise 11: Drop and Clear Stashes

bash
1# Drop one stash
2$ git stash drop stash@{0}
3Dropped stash@{0} (abc...)
4
5$ git stash list
6stash@{0}: On main: change 2
7stash@{1}: On main: change 1
8
9# Clear all remaining stashes
10$ git stash clear
11
12$ git stash list
13# (empty)

Exercise 12: Interactive Stashing

Make multiple changes:

bash
1$ git checkout -- app.js # Reset to clean state
2$ echo "keep this" >> app.js
3$ echo "stash this" >> README.md

Stash only README changes:

bash
1$ git stash push -p -m "selective stash"
2# Git will prompt for each hunk
3# Press 'y' for README.md changes
4# Press 'n' for app.js changes

Check the result:

bash
1$ git status
2Changes not staged for commit:
3 modified: app.js
4 # README.md changes are stashed
5
6$ git stash show
7 README.md | 1 +
8 1 file changed, 1 insertion(+)

Cleanup

bash
1$ cd ..
2$ rm -rf parallel-practice parallel-practice-hotfix parallel-practice-feature

Worktrees vs Stashing: When to Use Which

SituationBest Choice
Quick 5-minute context switchStash
Hotfix while working on featureWorktree
Running tests in backgroundWorktree
Saving work before risky operationStash
Multiple PRs in progressWorktrees
Brief branch checkStash

Checklist

  • Created a worktree for a branch
  • Listed all worktrees
  • Made commits in a worktree
  • Removed a worktree
  • Created a worktree with a new branch
  • Stashed uncommitted changes
  • Listed and showed stash contents
  • Applied and popped stashes
  • Managed multiple stashes
  • Used interactive stashing

Key Takeaway

Worktrees = separate directories for parallel work (heavy). Stashing = quick save/restore in same directory (light). Both prevent losing work when context switching.