Git Worktrees: A Comprehensive Tutorial
Git worktrees allow you to check out multiple branches of your repository simultaneously in separate directories. This is incredibly useful when you need to work on different branches without stashing or committing incomplete changes.
What Are Git Worktrees?
A worktree is an additional working directory linked to your repository. Each worktree can have its own branch checked out, allowing you to work on multiple branches concurrently without switching between them.
Prerequisites
- Git version 2.5 or newer
- Basic understanding of Git commands and branching
Basic Worktree Commands
Listing Worktrees
git worktree list
This shows your main working directory and any linked worktrees.
Adding a Worktree
git worktree add <path> <branch>
For example:
git worktree add ../feature-login feature/login
This creates a new directory at ../feature-login with the feature/login branch checked out.
Creating a New Branch with a Worktree
git worktree add -b <new-branch> <path> <start-point>
For example:
git worktree add -b bugfix/header ../bugfix-header main
Removing a Worktree
git worktree remove <path>
Or to force removal if the worktree contains modifications:
git worktree remove --force <path>
Pruning Worktrees
Remove references to worktrees whose directories no longer exist:
git worktree prune
Practical Workflow Example
-
Initial setup: You're working on the main branch in your repository.
-
Create a worktree for a feature:
git worktree add ../my-project-feature feature/new-ui
-
Navigate to the new worktree:
cd ../my-project-feature
-
Make changes in the feature branch worktree:
Make edits, commit them as usual.
-
Return to main worktree without stashing:
cd ../my-project
-
Continue working on main while your feature branch work remains intact in its own directory.
Best Practices
-
Use relative paths that are siblings to your main repository for easier navigation.
-
Name worktree directories clearly to indicate which branch they contain.
-
Prune regularly to clean up references to deleted worktrees:
git worktree prune
-
Be careful with shared branches - if you check out the same branch in multiple worktrees, changes in one will affect the others.
Common Issues and Solutions
"Path already exists"
If the target directory already exists, Git won't create a worktree there. Use a different path or remove the existing directory.
"is not a working tree"
This occurs when trying to remove a path that isn't a registered worktree. Check your worktree list.
Locked worktrees
If a worktree appears locked after its directory was removed:
git worktree prune
Advanced Usage
Temporary worktrees
For quick inspection of a commit:
git worktree add --detach ../temp-check <commit-hash>
Moving worktrees
Git doesn't support directly moving worktrees. Instead:
- Add a new worktree at the desired location
- Remove the old one
Conclusion
Git worktrees provide a powerful way to work on multiple branches simultaneously without the overhead of maintaining separate clones or constantly stashing changes. They're especially useful for:
- Working on a hotfix while keeping your feature branch work intact
- Reviewing pull requests without disrupting your current work
- Building or testing different branches simultaneously
With practice, worktrees can significantly improve your Git workflow efficiency.