Quick reference for all git worktree commands and common workflows.
git worktree list # Show all worktrees
git worktree list --porcelain # Machine-readable format# Create worktree with new branch
git worktree add <path> <new-branch>
# Create worktree from existing branch
git worktree add <path> <existing-branch>
# Create worktree from specific commit
git worktree add <path> <commit-hash>
# Create worktree with branch tracking remote
git worktree add <path> -b <branch> origin/<remote-branch>git worktree remove <path> # Remove worktree
git worktree remove --force <path> # Force remove (with uncommitted changes)git worktree prune # Clean up stale worktree references
git worktree prune --dry-run # Show what would be pruned# Current: working on feature branch with uncommitted changes
git worktree add ../project-hotfix main
cd ../project-hotfix
git checkout -b hotfix/critical-bug
# Fix bug, commit, merge to main
cd ../project # Return to feature work# Set up multiple feature streams
git worktree add ../project-auth feature/authentication
git worktree add ../project-ui feature/dashboard
git worktree add ../project-api feature/backend-api
# Work in parallel without context switching
cd ../project-auth # Work on auth
cd ../project-ui # Work on UI
cd ../project-api # Work on API# Dedicated review environment
git worktree add ../project-review main
cd ../project-review
# Always clean main branch for reviewing PRs# Multiple AI agents working simultaneously
git worktree add ../project-ai-frontend feature/ui-components
git worktree add ../project-ai-backend feature/api-backend
git worktree add ../project-ai-testing feature/test-suite
# Each AI agent works in isolation# Track remote branch in new worktree
git worktree add ../project-remote -b local-branch origin/remote-branch
# Push from worktree
cd ../project-feature
git push -u origin feature/new-feature# Quick experiment
git worktree add /tmp/experiment HEAD
cd /tmp/experiment
# Try something risky
cd -
git worktree remove /tmp/experiment# Create and switch in one command
git worktree add ../project-feature -b feature/new-feature
# Move worktree to different branch
cd ../project-feature
git checkout different-branch~/workspace/
βββ myproject/ # Main worktree (main branch)
βββ myproject-auth/ # Authentication feature
βββ myproject-dashboard/ # Dashboard feature
βββ myproject-hotfix/ # Emergency fixes
βββ myproject-review/ # Code review
~/workspace/myproject/
βββ main/ # Main branch
βββ features/
β βββ authentication/ # Auth feature
β βββ dashboard/ # Dashboard feature
β βββ payment/ # Payment feature
βββ hotfixes/
β βββ critical-bug/ # Hotfix
βββ review/ # Review branch
~/workspace/myproject/
βββ main/ # Human coordination
βββ agents/
β βββ frontend-ai/ # Frontend AI agent
β βββ backend-ai/ # Backend AI agent
β βββ testing-ai/ # Testing AI agent
β βββ docs-ai/ # Documentation AI agent
βββ integration/ # Integration branch
Add these to your ~/.gitconfig:
[alias]
# Worktree shortcuts
wt = worktree
wtls = worktree list
wtadd = worktree add
wtrm = worktree remove
wtprune = worktree prune
# Common workflows
hotfix = "!f() { git worktree add ../$(basename $(pwd))-hotfix main && cd ../$(basename $(pwd))-hotfix && git checkout -b hotfix/$1; }; f"
feature = "!f() { git worktree add ../$(basename $(pwd))-$1 -b feature/$1; }; f"
review = "!git worktree add ../$(basename $(pwd))-review main"
# Cleanup
wtclean = "!git worktree prune && echo 'Worktree references cleaned'"Usage:
git hotfix login-bug # Creates hotfix worktree and branch
git feature dashboard # Creates feature worktree and branch
git review # Creates review worktree
git wtclean # Cleans up referencesAdd to your ~/.bashrc or ~/.zshrc:
# Quick worktree navigation
wtcd() {
local worktree=$(git worktree list | fzf | awk '{print $1}')
if [ -n "$worktree" ]; then
cd "$worktree"
fi
}
# Worktree status overview
wtstatus() {
echo "π³ Worktree Status Overview"
echo "=========================="
echo
git worktree list
echo
echo "πΎ Disk Usage:"
du -sh ../*$(basename $(pwd))* 2>/dev/null | sort -hr
}
# Quick worktree creation
wtquick() {
local name=$1
local branch=${2:-feature/$name}
git worktree add "../$(basename $(pwd))-$name" -b "$branch"
cd "../$(basename $(pwd))-$name"
}# β This will fail
git worktree add ../project-feature feature/auth
git worktree add ../project-feature2 feature/auth # Error!
# β
Use different branches
git worktree add ../project-feature feature/auth
git worktree add ../project-feature2 feature/auth-v2# β Worktrees don't support Git LFS files
# If your repo uses LFS, files may not be available in worktrees# β οΈ Git hooks with relative paths may not work correctly
# Use absolute paths in hooks when using worktrees# Remove merged feature worktrees
for worktree in ../project-*; do
if [ -d "$worktree" ]; then
cd "$worktree"
branch=$(git branch --show-current)
if git merge-base --is-ancestor HEAD main; then
echo "Removing merged worktree: $worktree"
cd ..
git worktree remove "$worktree"
fi
cd - > /dev/null
fi
done# Prune orphaned references
git worktree prune
# List old worktrees
find .. -maxdepth 1 -name "*$(basename $(pwd))*" -type d -mtime +7# If you deleted the directory but worktree still listed
git worktree prune # Clean up the reference# If worktree has uncommitted changes
git worktree remove --force <path>
# Or commit/stash changes first
cd <worktree-path>
git add . && git commit -m "Save work"
cd -
git worktree remove <path>cd <worktree-path>
git checkout <correct-branch>
# or
git checkout -b <new-branch># On some systems, check permissions
chmod -R u+w <worktree-path>
git worktree remove <path>- Consistent Naming: Use prefixes like
project-feature-name - Regular Cleanup: Don't let worktrees accumulate
- Backup Important Work: Before removing worktrees with unique changes
- Use Absolute Paths: Avoid issues with relative paths
- Team Communication: Notify team when removing shared worktrees
Keep this cheatsheet handy for quick reference during your worktree workflows! π