Git Worktrees
Worktrees let you have multiple working directories attached to the same repository. Each worktree can check out a different branch simultaneously.
Why Worktrees?
Without worktrees, switching branches requires:
- Stashing or committing current work
- Switching branches
- Doing urgent work
- Switching back
- Restoring your original work
With worktrees:
- Each branch has its own directory
- No stashing or context switching needed
- Work on multiple branches truly in parallel
When to Use Worktrees
| Scenario | Worktrees Help? |
|---|---|
| Urgent hotfix during feature work | Yes |
| Running tests on one branch while coding on another | Yes |
| Comparing implementations across branches | Yes |
| Quick one-file edit | No (use stash) |
| Build processes that lock files | Yes |
Adding a Worktree
Basic Syntax
bash1$ git worktree add <path> <branch>
Example: Create Hotfix Worktree
bash1$ git worktree add ../project-hotfix hotfix/urgent-bug2Preparing worktree (checking out 'hotfix/urgent-bug')3HEAD is now at abc1234 Previous commit message
This creates:
- A new directory
../project-hotfix - Checked out to
hotfix/urgent-bugbranch - Shares the same
.gitdata as your main worktree
Create Worktree with New Branch
bash1$ git worktree add -b hotfix/new-fix ../project-hotfix main
This creates a new branch hotfix/new-fix from main in the new worktree.
Listing Worktrees
bash1$ git worktree list2/home/user/project abc1234 [main]3/home/user/project-hotfix def5678 [hotfix/urgent-bug]
You can run this from any worktree to see all connected working directories.
Working in a Worktree
Each worktree is a full working directory:
bash1$ cd ../project-hotfix2$ ls3README.md src/ package.json ...45# Make changes6$ vim src/bug.js7$ git add src/bug.js8$ git commit -m "fix: resolve urgent bug"9$ git push origin hotfix/urgent-bug
Meanwhile, your original worktree remains untouched on its branch.
Removing a Worktree
When done with a worktree:
bash1# Remove the worktree2$ git worktree remove ../project-hotfix34# Or if files were modified5$ git worktree remove --force ../project-hotfix
If you manually deleted the directory, clean up the Git metadata:
bash1$ git worktree prune
Worktree Constraints
One Branch Per Worktree
You cannot check out the same branch in multiple worktrees:
bash1$ git worktree add ../another-main main2fatal: 'main' is already checked out at '/home/user/project'
This prevents conflicting changes to the same branch.
Shared Git Directory
All worktrees share:
- Commit history
- Configuration
- Hooks
- Remote references
Each worktree has its own:
- Working directory
- Index (staging area)
- HEAD (current branch)
Advanced Usage
Worktree for Code Review
Temporarily check out a PR to review:
bash1$ git fetch origin pull/123/head:pr-1232$ git worktree add ../review-pr-123 pr-1233$ cd ../review-pr-1234# Review, test, run the code5$ cd ../project6$ git worktree remove ../review-pr-123
Worktree for Build
Run builds while continuing development:
bash1$ git worktree add ../project-build release/v2.02$ cd ../project-build3$ npm run build4# Build runs in this directory while you work elsewhere
Commands Summary
| Command | Purpose |
|---|---|
git worktree add <path> <branch> | Create new worktree |
git worktree add -b <new-branch> <path> <start> | Create worktree with new branch |
git worktree list | Show all worktrees |
git worktree remove <path> | Remove a worktree |
git worktree prune | Clean up stale worktree data |
Key Takeaway
Worktrees eliminate context-switching overhead for parallel work. Use them for hotfixes, reviews, builds, or any time you need multiple branches accessible simultaneously. Clean them up when done to avoid confusion.