Git worktrees
Recently, I started using Git worktrees as my projects grew to the point where builds could take a few minutes. Testing changes from different branches was slowing me down, and I was losing track of what I was working on.
Here are my notes after looking into them.
What is a worktree?
- A linked working directory attached to the same repository
- Each worktree can check out a different branch
- The repo shares one object database, history, and remotes
masterclone + as many worktrees as you need- Not a clone: no second copy of
.git, almost no extra disk usage
Why use them?
- Work on two branches at once without stashing
- Hotfix while feature work is in progress: fix it in another worktree, commit, push, go back
- Run long builds or tests on one branch while you keep coding on another
- Review a PR in a separate directory without touching your working tree
- Compare branches side by side in your editor
- Better than stashing: nothing is hidden, each branch keeps its real state on disk
Disadvantages
- Same branch, two worktrees: Git refuses it (which makes sense)
- Build artifacts don't carry over:
node_modules,.venvmust be reinstalled per worktree - Files ignored by Git must be copied:
.envfiles and similar files must be copied to each worktree - Editor/IDE confusion: multiple roots, duplicate indexes, per-directory tool state
Takeaways
- Worktrees = parallel branches, zero stashing
- Perfect for hotfixes, reviews, and long-running checks
- Cost: duplicated builds and dependencies
Cheat sheet
| Task | Command |
|---|---|
| Worktree with a new branch | git worktree add ../repo-feat -b feature-x |
| Worktree for an existing branch | git worktree add ../repo-hotfix hotfix-branch |
| Worktree at a specific commit/tag | git worktree add ../repo-v1 v1.0.0 |
| List all worktrees | git worktree list |
| Remove a worktree (must be clean) | git worktree remove ../repo-feat |
| Force-remove a dirty worktree | git worktree remove --force ../repo-feat |
| Move a worktree to a new location | git worktree move ../old ../new |
| Lock a worktree (prevent pruning) | git worktree lock ../repo-feat |
| Unlock a worktree | git worktree unlock ../repo-feat |
| Prune stale worktree metadata | git worktree prune |
| Show help | git worktree --help |
Bare repositories
It’s possible to use bare repositories too, but they don’t fit my workflow. That’s why I don’t mention them.