| name | git-worktree-manager |
| description | Manage git worktrees for efficient multi-branch development. Use when you need to create worktrees for feature branches, organize worktree directories, clean up unused worktrees, or implement worktree-based workflows. Use when this capability is needed. |
| metadata | {"author":"d-o-hub"} |
Git Worktree Manager
Manage git worktrees to enable efficient multi-branch development while keeping the main working directory clean.
Purpose
Enable parallel development on multiple branches without switching contexts, allowing isolation and organization of work across multiple features, bugfixes, and experiments.
When to Use
Use this skill when:
- Creating Worktrees: Setting up worktrees for new feature branches
- Organizing Worktrees: Structuring worktree directories for better management
- Cleaning Up: Removing unused or orphaned worktrees
- Multi-Branch Development: Working on multiple features simultaneously
- Isolated Development: Keeping experimental changes separate from main work
- CI/CD Workflows: Using worktrees for testing and deployment
- Worktree Maintenance: Listing, switching, and managing multiple worktrees
Worktree Fundamentals
What is a Git Worktree?
A git worktree is a linked working tree attached to the same repository. Each worktree allows you to:
- Check out different branches simultaneously
- Work on multiple features in parallel
- Keep main working directory clean
- Test changes without switching branches
Directory Structure
Recommended organization:
project/
├── main-repo/ # Main working directory (default)
└── worktrees/ # All worktrees organized here
├── feature/feature-1/
├── bugfix/bug-2/
├── hotfix/critical-3/
└── experiment/new-architecture/
Naming Conventions
- Branch-Based: Name worktrees after their branches
- Type Prefix: Use prefixes for organization (feature/, bugfix/, hotfix/, experiment/)
- Descriptive Names: Clear, meaningful names that indicate purpose
- No Special Characters: Avoid spaces, special characters in names
Core Operations
1. Create a Worktree
From Existing Branch
cd /path/to/project
git worktree add ../worktrees/feature-name feature-branch-name
cd ../worktrees/feature-name
git status
Create New Branch
git worktree add -b feature/new-feature ../worktrees/new-feature
cd ../worktrees/new-feature
Detached HEAD Worktree
git worktree add ../worktrees/temp-checkout HEAD~5
2. List Worktrees
Basic Listing
git worktree list
git worktree list --porcelain
Detailed Status Check
for wt in $(git worktree list --porcelain | grep worktree | sed 's/worktree //'); do
echo "=== $wt ==="
cd "$wt" && git status --short && git log --oneline -1
done
3. Navigate Between Worktrees
Quick Navigation
goto-worktree() {
local wt=$1
local project_root=$(git rev-parse --show-toplevel)
local worktree_path="$project_root/../worktrees/$wt"
if [ -d "$worktree_path" ]; then
cd "$worktree_path"
echo "Switched to worktree: $wt"
else
echo "Worktree not found: $wt"
fi
}
Use with Projects
alias gwt='goto-worktree'
gwt feature-name
4. Manage Worktree Status
Lock and Unlock
git worktree lock ../worktrees/feature-name
git worktree unlock ../worktrees/feature-name
git worktree list
Prune Worktrees
git worktree prune
git worktree prune -v
Worktree Cleanup
Identify Unused Worktrees
Check for Orphaned Worktrees
git worktree list | while read path commit branch; do
if ! git show-ref --verify --quiet "refs/heads/${branch#*/}"; then
echo "Orphaned: $path ($branch)"
fi
done
Check for Uncommitted Changes
for wt in $(git worktree list --porcelain | grep worktree | sed 's/worktree //'); do
if [ -n "$(cd "$wt" && git status --porcelain)" ]; then
echo "Uncommitted changes: $wt"
fi
done
Safe Removal
Remove a Worktree
git worktree remove ../worktrees/feature-name
git worktree remove --force ../worktrees/feature-name
Remove Multiple Worktrees
for wt in ../worktrees/feature-*; do
git worktree remove "$wt"
done
Workflow Integration
Feature Development Workflow
1. Create worktree for new feature
```bash
git worktree add -b feature/new-ui ../worktrees/feature/new-ui
-
Develop feature in isolation
cd ../worktrees/feature/new-ui
-
Test changes locally
cargo test
-
Commit and push to branch
git add .
git commit -m "Implement new UI"
git push
-
Merge back to main
cd /path/to/main-repo
git checkout main
git merge feature/new-ui
-
Clean up worktree
git worktree remove ../worktrees/feature/new-ui
### Parallel Testing Workflow
```bash
# Test multiple branches simultaneously
for branch in feature/a feature/b feature/c; do
git worktree add ../worktrees/$branch $branch
cd ../worktrees/$branch
cargo test &
done
# Wait for all tests to complete
wait
Bugfix Workflow
git worktree add -b hotfix/critical-issue ../worktrees/hotfix/critical-issue
cd ../worktrees/hotfix/critical-issue
cargo test
git checkout release/v1.2
git merge hotfix/critical-issue
git worktree remove ../worktrees/hotfix/critical-issue
CI/CD Integration
- name: Test with Worktrees
run: |
# Create worktree for testing
git worktree add /tmp/ci-test $GITHUB_SHA
cd /tmp/ci-test
cargo test --all
rm -rf /tmp/ci-test
Code Review Workflow
git worktree add ../worktrees/review/pr-123 origin/pr-123
cd ../worktrees/review/pr-123
cargo test
git worktree remove ../worktrees/review/pr-123
Git Configuration
Worktree-Specific Configuration
cd ../worktrees/feature-name
git config user.name "Developer Name"
git config user.email "developer@example.com"
git config branch.feature-name.mergeoptions "no-ff"
Aliases for Common Operations
[alias]
wt-list = worktree list
wt-add = "!f() { git worktree add ../worktrees/$1 ${2:--b $2}; }; f"
wt-remove = "!f() { git worktree remove ../worktrees/$1; }; f"
wt-prune = worktree prune
Best Practices
DO:
✓ Organize worktrees in dedicated directory (../worktrees/)
✓ Use descriptive branch names and worktree paths
✓ Clean up worktrees after merging or abandoning work
✓ Test in worktree before merging to main branch
✓ Use worktrees for isolated, parallel development
✓ Prune worktrees regularly to remove orphaned data
✓ Lock worktrees during automated operations
✓ Verify worktree status before major operations
DON'T:
✗ Create worktrees in random locations
✗ Use worktrees as long-term storage (use branches instead)
✗ Leave worktrees with uncommitted changes indefinitely
✗ Forget to prune worktree administrative data
✗ Create worktrees with conflicting branches
✗ Mix worktree and non-worktree workflows without clear separation
✗ Remove worktrees without checking for uncommitted changes
✗ Use worktrees for unrelated changes (keep them focused)
Advanced Patterns
Main Branch Protection
Worktree Isolation
Stashing in Worktrees
cd ../worktrees/feature-a
git stash save "WIP feature A"
cd ../worktrees/feature-b
git stash save "WIP feature B"
Submodule Considerations
Troubleshooting
Common Issues
Issue: Worktree Already Exists
git worktree remove ../worktrees/feature-name
git worktree add ../worktrees/feature-name feature-branch-name
Issue: Detached HEAD
cd ../worktrees/feature-name
git checkout feature-branch-name
Issue: Merge Conflicts
cd ../worktrees/feature-name
git merge main
git commit
git checkout main
git merge feature-name
Issue: Large .git Directory
git worktree prune
Tools and Commands Reference
Quick Reference
git worktree list
git worktree add ../worktrees/name branch-name
git worktree add -b new-branch ../worktrees/name
git worktree remove ../worktrees/name
git worktree remove --force ../worktrees/name
git worktree prune
git worktree move ../worktrees/old-path ../worktrees/new-path
git worktree lock ../worktrees/name
git worktree unlock ../worktrees/name
Status Checking
git worktree list
git worktree list --porcelain
git worktree list | grep feature-branch-name
Integration with Development Workflows
With IDE
With Testing
for branch in $(git branch -r | grep -v HEAD); do
git worktree add /tmp/test-$branch $branch
cd /tmp/test-$branch && cargo test
rm -rf /tmp/test-$branch
done
With CI/CD
- name: Parallel Testing
run: |
for branch in test-1 test-2 test-3; do
git worktree add /tmp/$branch origin/$branch
cd /tmp/$branch && cargo test
rm -rf /tmp/$branch
done
Maintenance
Regular Cleanup Schedule
- Daily: Prune worktrees
- Weekly: Check for orphaned worktrees
- Monthly: Review and clean up old worktree directories
Monitoring
git worktree list | wc -l
du -sh ../worktrees/
du -sh .git
Backup Considerations
Summary
Git worktrees enable:
- Parallel Development: Multiple branches simultaneously
- Isolation: Clean main working directory
- Flexibility: Easy to create and remove workspaces
- Organization: Structured worktree management
- Efficiency: Faster context switching between features
Use worktrees for efficient, isolated multi-branch development while maintaining a clean main working directory.
Source: d-o-hub/rust-self-learning-memory — distributed by TomeVault.