Duration ~ 15 min
Level: beginner-friendly
github
worktrees.
Work on multiple branches at once without juggling stashes, clones, or context-switching tax. A practical primer for people who already use git.
One working directory is a bottleneck.
You are halfway through a feature on feature/login. A teammate pings: "the build is broken on main, can you look?"
Now you stash your work, switch branches, investigate, switch back, pop the stash, and hope nothing collided. Multiply by ten interruptions a week.
Worktrees sidestep the whole dance.
A worktree is a second checkout of the same repo.
Same .git, same history, same remotes. A separate folder on disk where a different branch lives, ready to work in. No clone, no stash, no ceremony.
Three words, one new folder.
The first form creates a new folder one level up, checks out the current branch's tip into a new branch, and wires it to the shared .git.
The -b flag lets you name the branch at the same time. Common when you want a clean branch for the work.
Open the new folder in a second editor window. That is the whole workflow.
Stash-and-switch vs. worktree.
- Open a second folder, keep coding
- Both branches visible at once
- No stash, no pop, no conflicts
- Editor state preserved per branch
- Tests can run in parallel
- Switching is instant - it is just a window
- Stash, switch, investigate, switch back
- One branch visible at a time
- Stash pop can collide
- Editor tabs and scroll reset each time
- Long-running tasks block the branch
- Each switch reloads context into your head
A worktree is temporary by design.
Removing a worktree deletes the folder and detaches it from .git. The branch you worked on stays in the repo until you delete it explicitly.
If you just delete the folder by hand, git remembers a stale entry. Run git worktree prune to tidy up.
Reach for a worktree when...
A hotfix lands mid-feature
You are deep in a refactor. Production breaks. Open a worktree on main, fix, ship, return - your feature branch has not moved.
You review a teammate's PR
Check out their branch into a worktree. Run it, poke the code, keep your own work open beside it. No branch juggling.
Long-running tasks block you
A migration is running on your branch. A test suite takes ten minutes. Spin up a worktree, keep working, let the slow thing finish in its own folder.