Grow a New Tree with Git Worktrees
Feb 23, 2026 · 5 min read
The Interruption
I was deep in a refactor. The screen was a blur of green and red. My brain was locked on one goal.
Then Slack pinged. A production bug. The kind that makes my heart jump. The fix was needed right now.
I ran git status and felt my stomach drop. Uncommitted changes everywhere. Half done work. Files moved around. Experiments I did not want to explain to anyone.
I did what people do in a panic. I made a quick branch and dumped my mess there, just so I could switch to the urgent fix. When I came back, I hit a merge conflict with my own unfinished work. I was fighting my past self, and I was losing.
Note Git Worktrees fixed this problem for me. They let me keep my feature work exactly as it is, and still jump to an urgent task in a clean folder.
What is a Git Worktree?
In a normal Git setup, you have one working folder. When you switch branches, Git swaps the files inside that same folder.
A Git worktree lets one repository have more than one working folder. Each folder is tied to one branch or one commit. You do not have to clone the repo again. The folders share the same Git history stored in the .git area, so they stay light and fast.
project-directory/
├── .git/
├── myrepo_main/ <-- Your main working branch
├── myrepo_feature_auth/ <-- Worktree 1 (Feature)
└── myrepo_hotfix/ <-- Worktree 2 (The bug fix)
Think of it like this: Instead of one desk where you keep clearing space for a new task, you get a second desk that already has the right files on it.
Tip Quick Glossary:
- Branch: A separate line of work.
- Commit: A saved checkpoint.
- Stash: A temporary place to park changes.
The Hotfix Moment, Solved
Here is the flow that keeps your feature work safe and your hotfix clean.
- Stay in your feature folder. Do not stash. Do not commit. Just leave your changes where they are.
- Create a new worktree in a separate folder for the hotfix. This example creates a new branch called
hotfix/prod123frommain.
git worktree add -b hotfix/prod123 ../myrepo_hotfix main
(If your default branch is master, replace main with master.)
- Move into the new folder and do the fix.
cd ../myrepo_hotfix
- Commit and push the fix like normal.
git status
# edit files, run tests
git commit -am "Fix production bug"
git push -u origin hotfix/prod123
- Remove the worktree folder when the emergency is over.
git worktree remove ../myrepo_hotfix
Go back to your feature folder. Your files are still exactly as you left them.
The Main Commands
You only need a few commands to get value from worktrees.
Create a worktree
Create a new branch for the worktree (common for hotfixes):
git worktree add -b <new-branch> <path> <base-branch>
Use an existing branch (common for reviews):
git worktree add <path> <existing-branch>
See all worktrees
git worktree list
Remove a worktree
git worktree remove <path>
Clean up stale entries
If you delete a worktree folder by hand, Git can keep a record of it. prune removes those leftovers.
git worktree prune
Best Times to Use Worktrees
- The Urgent Hotfix: You are mid feature and a production fix shows up. Keep your feature folder messy and open a clean hotfix folder.
- Code Reviews: You need to review a teammate's branch. Open it in a new folder, run it, and close it when done.
- Parallel Execution: You want to run a slow build or test run and keep coding at the same time in another folder.
- A/B Testing Local: You want to compare two versions of the app side by side on your machine. Put each version in its own folder.
Rules That Keep This Stress-Free
- One branch per worktree. A branch can only be checked out in one worktree at a time. This helps avoid accidents.
- Create worktree folders next to your main repo folder, not inside it. This keeps things easy to see and easy to delete.
- Name your folders explicitly so you know what they are for (e.g.,
myrepo_hotfixormyrepo_review). - Remove worktrees when you are done with them. Your history stays safe in the main repository.
- Mistakes happen. If a worktree folder is deleted by mistake, run
git worktree pruneto clean up the record.
Extra Tricks You May Like
These are optional, but they can be useful once you get comfortable.
Make a temporary worktree
If you just want a scratch folder for quick testing, you can create a worktree that is not tied to a branch.
git worktree add --detach ../myrepo_scratch
Lock a worktree
If a worktree lives on a drive that is not always plugged in, you can lock it so Git does not prune its record.
git worktree lock <path>
git worktree unlock <path>
Move or repair a worktree
If you want to relocate a worktree folder, use move. If paths get out of sync, repair can help.
git worktree move <old-path> <new-path>
git worktree repair
Wrap Up
Important Worktrees turned my worst context switch into a simple routine.
I keep my feature mess untouched. I open a clean folder for the urgent fix. I ship it. Then I delete that folder and go right back to where I was.
Next time the hotfix message hits, you do not need to gamble with stashes or messy commits. Just grow a new tree.
Recommended Resources
Note Further Reading: