Compare Branches Side by Side with Worktrees
Debug differences or run benchmarks by having multiple branches checked out simultaneously.
When to use
You need to compare behavior, outputs, or performance between two branches. Having both checked out simultaneously lets you run them side by side without constant branch switching.
Pre-flight
- Identify the two branches you want to compare (e.g.,
mainvsfeat/new-algorithm). - Ensure both branches are up to date with the remote.
Steps
-
Keep your primary worktree on one branch (e.g., main).
git checkout main git pull origin main -
Create a worktree for the branch you want to compare against.
git worktree add ../feature-comparison feat/new-algorithm -
Run your comparison tasks in separate terminals.
# Terminal 1: Main branch cd ~/project npm run benchmark > main-results.txt # Terminal 2: Feature branch cd ~/feature-comparison npm run benchmark > feature-results.txt -
Compare outputs using diff or your preferred comparison tool.
diff main-results.txt ../feature-comparison/feature-results.txt -
Clean up when done.
git worktree remove ../feature-comparison
Verification
- Both branches run independently with their own outputs.
- Comparison data helps identify behavioral differences or performance regressions.
Use case: Visual regression testing
Run both versions of a web app on different ports and compare visually:
# Main branch on port 3000
cd ~/project && PORT=3000 npm start
# Feature branch on port 3001
cd ~/feature-comparison && PORT=3001 npm start
Open both in your browser to compare UI changes side by side.
Follow-up
- Document any significant differences found during comparison.
- If benchmarking, run multiple iterations to account for variance.