Ever been juggling a bunch of branches or changes in Git and wished for a smoother ride? Say hello to Git Worktree.
Git Worktree lets you check out multiple branches at once within a single repository. This means you can tinker on different branches simultaneously without switching contexts or stashing your changes.
Here’s how:
git worktree add # Create a new worktree
git worktree list # List all worktrees
git worktree remove # Remove a worktree
Comparing Git Worktree and Git Stash
Stash hop vs worktree
One working tree vs true parallel checkouts
Idea: stash parks work and swaps context. Worktree keeps both branches live on disk so you hop folders, not mental stacks.
True Parallel Development
Git Worktree
- Lets you have multiple branches checked out at the same time in different directories.
- This reduces the need for constant context switching.
- Cleaner mental model as your file system mirrors your Git branching strategy.
project/
├── main/
├── feature-a/
└── bugfix-123/
Git Stash
- You have to stash your changes, switch branches, and then reapply them, which can be a bit cumbersome.
- Doesn’t allow true parallel work on multiple branches.
Workflow & Time Efficiency
Git Worktree
- Perfect for tasks that require working on multiple features or bug fixes concurrently.
- You can hop between branches without breaking a sweat.
- Makes code reviews a breeze by keeping the PR branch right alongside the main branch.
Git Stash
- Great for quick switches, but you’ve got to stash and pop changes
- Can throw a spanner in the works. Kills the momentum.
Metrics
Imagine this: you’re knee-deep in feature A but need to fix a bug in feature B.
Git Worktree
- Checking out a new worktree for the bug fix and switching back to feature A after fixing it takes around 10 seconds. Seriously!
- Daily time: 10 seconds * 5 = 50 seconds
- Monthly time (20 working days): 50 seconds * 20 = 16.67 minutes
Git Stash
- Stashing changes, switching branches, and popping the stash… you know how it goes.
- Time to stash, switch branches, and pop stash: ~1 minute
- Daily time: 1 minute * 5 = 5 minutes
- Monthly time (20 working days): 5 minutes * 20 = 100 minutes
Here’s what a worktree workflow would look like:
# Create a new worktree
git worktree add ../feature-branch feature-branch
# Switch to the new worktree directory
cd ../feature-branch
# Work on your changes, commit as usual
git add .
git commit -m "Implement new feature"
# Return to the main worktree
cd -
# List all worktrees
git worktree list
# Remove a worktree when no longer needed
git worktree remove ../feature-branch
And here’s what a stash workflow looks like:
# Stash current changes
git stash save "Work in progress on feature X"
# Switch branch or context
git checkout another-branch
# Do some work, then return to original branch
git checkout original-branch
# Apply the stashed changes
git stash apply
# Or pop the stash (apply and remove from stash list)
git stash pop
# List all stashes
git stash list
# Clear all stashes
git stash clear
Conclusion
Git Worktree can save you a ton of time, especially when you’re juggling multiple branches and frequent context switches. While Git Stash is useful for quick changes, Git Worktree offers a more efficient workflow for handling concurrent tasks.
So, the next time you’re bouncing between branches, give Git Worktree a try. It might just be the game-changer you need to boost your productivity.
Happy coding!
Originally published on Medium.