git-worktree-manager
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Worktree Manager
Git Worktree 管理器
Manage git worktrees to enable efficient multi-branch development while keeping the main working directory clean.
管理Git worktree,在保持主工作目录整洁的同时实现高效的多分支开发。
Purpose
用途
Enable parallel development on multiple branches without switching contexts, allowing isolation and organization of work across multiple features, bugfixes, and experiments.
无需切换上下文即可在多个分支上并行开发,实现多个功能、Bug修复和实验工作的隔离与管理。
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:为新功能分支创建worktree
- 整理Worktree:构建worktree目录结构以优化管理
- 清理工作:移除未使用或孤立的worktree
- 多分支开发:同时开发多个功能
- 隔离开发:将实验性修改与主工作内容分离
- CI/CD工作流:使用worktree进行测试与部署
- Worktree维护:列出、切换及管理多个worktree
Worktree Fundamentals
Worktree 基础概念
What is a Git Worktree?
什么是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
Git worktree是附加到同一仓库的关联工作树。每个worktree允许你:
- 同时检出不同分支
- 并行开发多个功能
- 保持主工作目录整洁
- 无需切换分支即可测试修改
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/推荐的组织方式:
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
- 基于分支命名:worktree名称与对应分支一致
- 类型前缀:使用前缀进行分类(feature/, bugfix/, hotfix/, experiment/)
- 描述性名称:清晰、有意义的名称,明确其用途
- 无特殊字符:名称中避免空格和特殊字符
Core Operations
核心操作
1. Create a Worktree
1. 创建Worktree
From Existing Branch
基于现有分支创建
bash
undefinedbash
undefinedNavigate to main repository
Navigate to main repository
cd /path/to/project
cd /path/to/project
Create worktree from existing branch
Create worktree from existing branch
git worktree add ../worktrees/feature-name feature-branch-name
git worktree add ../worktrees/feature-name feature-branch-name
Verify creation
Verify creation
cd ../worktrees/feature-name
git status
undefinedcd ../worktrees/feature-name
git status
undefinedCreate New Branch
创建新分支并生成Worktree
bash
undefinedbash
undefinedCreate worktree with new branch
Create worktree with new branch
git worktree add -b feature/new-feature ../worktrees/new-feature
git worktree add -b feature/new-feature ../worktrees/new-feature
Switch to worktree
Switch to worktree
cd ../worktrees/new-feature
undefinedcd ../worktrees/new-feature
undefinedDetached HEAD Worktree
分离HEAD的Worktree
bash
undefinedbash
undefinedCreate worktree at specific commit
Create worktree at specific commit
git worktree add ../worktrees/temp-checkout HEAD~5
undefinedgit worktree add ../worktrees/temp-checkout HEAD~5
undefined2. List Worktrees
2. 列出Worktree
Basic Listing
基础列表
bash
undefinedbash
undefinedList all worktrees
List all worktrees
git worktree list
git worktree list
List with porcelain format (machine-readable)
List with porcelain format (machine-readable)
git worktree list --porcelain
undefinedgit worktree list --porcelain
undefinedDetailed Status Check
详细状态检查
bash
undefinedbash
undefinedCheck status of all worktrees
Check status of all worktrees
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
undefinedfor wt in $(git worktree list --porcelain | grep worktree | sed 's/worktree //'); do
echo "=== $wt ==="
cd "$wt" && git status --short && git log --oneline -1
done
undefined3. Navigate Between Worktrees
3. 在Worktree间切换
Quick Navigation
快速导航
bash
undefinedbash
undefinedFunction to navigate to worktree
Function to navigate to worktree
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}
undefinedgoto-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}
undefinedUse with Projects
项目中使用
bash
undefinedbash
undefinedIn .bashrc or .zshrc
In .bashrc or .zshrc
alias gwt='goto-worktree'
alias gwt='goto-worktree'
Then use:
Then use:
gwt feature-name
undefinedgwt feature-name
undefined4. Manage Worktree Status
4. 管理Worktree状态
Lock and Unlock
锁定与解锁
bash
undefinedbash
undefinedLock a worktree (prevent operations)
Lock a worktree (prevent operations)
git worktree lock ../worktrees/feature-name
git worktree lock ../worktrees/feature-name
Unlock a worktree
Unlock a worktree
git worktree unlock ../worktrees/feature-name
git worktree unlock ../worktrees/feature-name
Check locked status
Check locked status
git worktree list
undefinedgit worktree list
undefinedPrune Worktrees
清理Worktree管理数据
bash
undefinedbash
undefinedClean up worktree administrative data
Clean up worktree administrative data
git worktree prune
git worktree prune
Prune with verbose output
Prune with verbose output
git worktree prune -v
undefinedgit worktree prune -v
undefinedWorktree Cleanup
Worktree 清理
Identify Unused Worktrees
识别未使用的Worktree
Check for Orphaned Worktrees
检查孤立Worktree
bash
undefinedbash
undefinedFind worktrees with deleted branches
Find worktrees with deleted branches
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
undefinedgit worktree list | while read path commit branch; do
if ! git show-ref --verify --quiet "refs/heads/${branch#*/}"; then
echo "Orphaned: $path ($branch)"
fi
done
undefinedCheck for Uncommitted Changes
检查未提交修改
bash
undefinedbash
undefinedList worktrees with uncommitted changes
List worktrees with 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
undefinedfor 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
undefinedSafe Removal
安全移除
Remove a Worktree
移除单个Worktree
bash
undefinedbash
undefinedSafe removal (if clean)
Safe removal (if clean)
git worktree remove ../worktrees/feature-name
git worktree remove ../worktrees/feature-name
Force removal (ignores uncommitted changes)
Force removal (ignores uncommitted changes)
git worktree remove --force ../worktrees/feature-name
undefinedgit worktree remove --force ../worktrees/feature-name
undefinedRemove Multiple Worktrees
批量移除Worktree
bash
undefinedbash
undefinedRemove all worktrees matching pattern
Remove all worktrees matching pattern
for wt in ../worktrees/feature-*; do
git worktree remove "$wt"
done
undefinedfor wt in ../worktrees/feature-*; do
git worktree remove "$wt"
done
undefinedWorkflow Integration
工作流集成
Feature Development Workflow
功能开发工作流
markdown
1. Create worktree for new feature
```bash
git worktree add -b feature/new-ui ../worktrees/feature/new-ui-
Develop feature in isolationbash
cd ../worktrees/feature/new-ui # ... develop feature ... -
Test changes locallybash
cargo test -
Commit and push to branchbash
git add . git commit -m "Implement new UI" git push -
Merge back to mainbash
cd /path/to/main-repo git checkout main git merge feature/new-ui -
Clean up worktreebash
git worktree remove ../worktrees/feature/new-ui
undefinedmarkdown
1. Create worktree for new feature
```bash
git worktree add -b feature/new-ui ../worktrees/feature/new-ui-
Develop feature in isolationbash
cd ../worktrees/feature/new-ui # ... develop feature ... -
Test changes locallybash
cargo test -
Commit and push to branchbash
git add . git commit -m "Implement new UI" git push -
Merge back to mainbash
cd /path/to/main-repo git checkout main git merge feature/new-ui -
Clean up worktreebash
git worktree remove ../worktrees/feature/new-ui
undefinedParallel Testing Workflow
并行测试工作流
bash
undefinedbash
undefinedTest multiple branches simultaneously
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
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 for all tests to complete
wait
undefinedwait
undefinedBugfix Workflow
Bug修复工作流
bash
undefinedbash
undefinedCreate worktree for urgent bugfix
Create worktree for urgent bugfix
git worktree add -b hotfix/critical-issue ../worktrees/hotfix/critical-issue
git worktree add -b hotfix/critical-issue ../worktrees/hotfix/critical-issue
Fix and test in isolation
Fix and test in isolation
cd ../worktrees/hotfix/critical-issue
cd ../worktrees/hotfix/critical-issue
... fix bug ...
... fix bug ...
Test thoroughly
Test thoroughly
cargo test
cargo test
Merge to release branch
Merge to release branch
git checkout release/v1.2
git merge hotfix/critical-issue
git checkout release/v1.2
git merge hotfix/critical-issue
Clean up
Clean up
git worktree remove ../worktrees/hotfix/critical-issue
undefinedgit worktree remove ../worktrees/hotfix/critical-issue
undefinedCI/CD Integration
CI/CD集成
yaml
undefinedyaml
undefinedGitHub Actions workflow example
GitHub Actions workflow example
-
name: Test with Worktrees run: |
Create worktree for testing
git worktree add /tmp/ci-test $GITHUB_SHARun tests in worktree
cd /tmp/ci-test cargo test --allClean up
rm -rf /tmp/ci-test
undefined-
name: Test with Worktrees run: |
Create worktree for testing
git worktree add /tmp/ci-test $GITHUB_SHARun tests in worktree
cd /tmp/ci-test cargo test --allClean up
rm -rf /tmp/ci-test
undefinedCode Review Workflow
代码评审工作流
bash
undefinedbash
undefinedCreate worktree for PR review
Create worktree for PR review
git worktree add ../worktrees/review/pr-123 origin/pr-123
git worktree add ../worktrees/review/pr-123 origin/pr-123
Review changes in isolation
Review changes in isolation
cd ../worktrees/review/pr-123
cd ../worktrees/review/pr-123
... review code ...
... review code ...
Run tests
Run tests
cargo test
cargo test
Clean up after review
Clean up after review
git worktree remove ../worktrees/review/pr-123
undefinedgit worktree remove ../worktrees/review/pr-123
undefinedGit Configuration
Git配置
Worktree-Specific Configuration
Worktree专属配置
bash
undefinedbash
undefinedConfigure behavior in specific worktree
Configure behavior in specific worktree
cd ../worktrees/feature-name
cd ../worktrees/feature-name
Set up local Git config
Set up local Git config
git config user.name "Developer Name"
git config user.email "developer@example.com"
git config user.name "Developer Name"
git config user.email "developer@example.com"
Configure branch-specific settings
Configure branch-specific settings
git config branch.feature-name.mergeoptions "no-ff"
undefinedgit config branch.feature-name.mergeoptions "no-ff"
undefinedAliases for Common Operations
常用操作别名
bash
undefinedbash
undefinedAdd to ~/.gitconfig
Add to ~/.gitconfig
[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
undefined[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
undefinedBest 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
✓ 将worktree组织在专用目录中(../worktrees/)
✓ 使用描述性的分支名称和worktree路径
✓ 合并或放弃工作后清理worktree
✓ 合并到主分支前在worktree中进行测试
✓ 使用worktree进行隔离的并行开发
✓ 定期清理worktree以移除孤立数据
✓ 自动化操作期间锁定worktree
✓ 执行重大操作前验证worktree状态
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)
✗ 在随机位置创建worktree
✗ 将worktree用作长期存储(改用分支)
✗ 让worktree长期存在未提交修改
✗ 忘记清理worktree的管理数据
✗ 创建分支冲突的worktree
✗ 无明确区分地混合worktree与非worktree工作流
✗ 未检查未提交修改就移除worktree
✗ 使用worktree处理不相关的修改(保持专注)
Advanced Patterns
高级模式
Main Branch Protection
主分支保护
bash
undefinedbash
undefinedKeep main branch clean
Keep main branch clean
Never commit directly to main from worktree
Never commit directly to main from worktree
Always create feature branches from main
Always create feature branches from main
Merge feature branches into main from main checkout
Merge feature branches into main from main checkout
Example safe workflow:
Example safe workflow:
1. In main: create-feature-branch
1. In main: create-feature-branch
2. In worktree: develop and test
2. In worktree: develop and test
3. In main: merge-feature-branch
3. In main: merge-feature-branch
4. Remove worktree
4. Remove worktree
undefinedundefinedWorktree Isolation
Worktree隔离
bash
undefinedbash
undefinedEach worktree is isolated environment
Each worktree is isolated environment
No shared state between worktrees
No shared state between worktrees
Each worktree has its own .git/config
Each worktree has its own .git/config
Changes in worktree don't affect main until merged
Changes in worktree don't affect main until merged
undefinedundefinedStashing in Worktrees
Worktree中的暂存操作
bash
undefinedbash
undefinedStash works independently in each worktree
Stash works independently in each worktree
cd ../worktrees/feature-a
git stash save "WIP feature A"
cd ../worktrees/feature-b
git stash save "WIP feature B"
undefinedcd ../worktrees/feature-a
git stash save "WIP feature A"
cd ../worktrees/feature-b
git stash save "WIP feature B"
undefinedSubmodule Considerations
子模块注意事项
bash
undefinedbash
undefinedWorktrees interact with submodules carefully
Worktrees interact with submodules carefully
Each worktree has its own submodule checkout
Each worktree has its own submodule checkout
Be aware of submodule init/update operations
Be aware of submodule init/update operations
undefinedundefinedTroubleshooting
故障排除
Common Issues
常见问题
Issue: Worktree Already Exists
问题:Worktree已存在
bash
undefinedbash
undefinedError: fatal: 'worktrees/feature-name' already exists
Error: fatal: 'worktrees/feature-name' already exists
Solution: Remove existing worktree first
Solution: Remove existing worktree first
git worktree remove ../worktrees/feature-name
git worktree remove ../worktrees/feature-name
Then recreate
Then recreate
git worktree add ../worktrees/feature-name feature-branch-name
undefinedgit worktree add ../worktrees/feature-name feature-branch-name
undefinedIssue: Detached HEAD
问题:分离HEAD状态
bash
undefinedbash
undefinedWorktree in detached HEAD state
Worktree in detached HEAD state
Solution: Check out appropriate branch
Solution: Check out appropriate branch
cd ../worktrees/feature-name
git checkout feature-branch-name
undefinedcd ../worktrees/feature-name
git checkout feature-branch-name
undefinedIssue: Merge Conflicts
问题:合并冲突
bash
undefinedbash
undefinedConflicts when merging from worktree
Conflicts when merging from worktree
Solution: Resolve in worktree, then merge
Solution: Resolve in worktree, then merge
cd ../worktrees/feature-name
git merge main
cd ../worktrees/feature-name
git merge main
Resolve conflicts
Resolve conflicts
git commit
git checkout main
git merge feature-name
undefinedgit commit
git checkout main
git merge feature-name
undefinedIssue: Large .git Directory
问题:.git目录过大
bash
undefinedbash
undefinedWorktrees can bloat .git directory
Worktrees can bloat .git directory
Solution: Use prune regularly
Solution: Use prune regularly
git worktree prune
git worktree prune
Consider using sparse checkout for large repos
Consider using sparse checkout for large repos
undefinedundefinedTools and Commands Reference
工具与命令参考
Quick Reference
快速参考
bash
undefinedbash
undefinedList all worktrees
List all worktrees
git worktree list
git worktree list
Create from branch
Create from branch
git worktree add ../worktrees/name branch-name
git worktree add ../worktrees/name branch-name
Create with new branch
Create with new branch
git worktree add -b new-branch ../worktrees/name
git worktree add -b new-branch ../worktrees/name
Remove worktree
Remove worktree
git worktree remove ../worktrees/name
git worktree remove ../worktrees/name
Force remove
Force remove
git worktree remove --force ../worktrees/name
git worktree remove --force ../worktrees/name
Prune administrative data
Prune administrative data
git worktree prune
git worktree prune
Move a worktree
Move a worktree
git worktree move ../worktrees/old-path ../worktrees/new-path
git worktree move ../worktrees/old-path ../worktrees/new-path
Lock worktree
Lock worktree
git worktree lock ../worktrees/name
git worktree lock ../worktrees/name
Unlock worktree
Unlock worktree
git worktree unlock ../worktrees/name
undefinedgit worktree unlock ../worktrees/name
undefinedStatus Checking
状态检查
bash
undefinedbash
undefinedCheck all worktree statuses
Check all worktree statuses
git worktree list
git worktree list
Get detailed information
Get detailed information
git worktree list --porcelain
git worktree list --porcelain
Find worktree for a branch
Find worktree for a branch
git worktree list | grep feature-branch-name
undefinedgit worktree list | grep feature-branch-name
undefinedIntegration with Development Workflows
与开发工作流集成
With IDE
与IDE集成
bash
undefinedbash
undefinedVS Code: Add multiple workspace roots
VS Code: Add multiple workspace roots
Settings -> Workspace -> Add Folder...
Settings -> Workspace -> Add Folder...
Add: /path/to/project, /path/to/worktrees/*
Add: /path/to/project, /path/to/worktrees/*
JetBrains: Configure multiple project roots
JetBrains: Configure multiple project roots
File -> Open -> Add Directory to Project
File -> Open -> Add Directory to Project
undefinedundefinedWith Testing
与测试集成
bash
undefinedbash
undefinedParallel test execution
Parallel test execution
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
undefinedfor 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
undefinedWith CI/CD
与CI/CD集成
yaml
undefinedyaml
undefinedGitHub Actions: Use worktrees for isolated testing
GitHub Actions: Use worktrees for isolated testing
- 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
undefined- 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
undefinedMaintenance
维护
Regular Cleanup Schedule
定期清理计划
- Daily: Prune worktrees
- Weekly: Check for orphaned worktrees
- Monthly: Review and clean up old worktree directories
- 每日:清理worktree管理数据
- 每周:检查孤立worktree
- 每月:检查并清理旧worktree目录
Monitoring
监控
bash
undefinedbash
undefinedMonitor worktree usage
Monitor worktree usage
git worktree list | wc -l # Count active worktrees
git worktree list | wc -l # Count active worktrees
Monitor disk usage
Monitor disk usage
du -sh ../worktrees/
du -sh ../worktrees/
Check for large .git directories
Check for large .git directories
du -sh .git
undefineddu -sh .git
undefinedBackup Considerations
备份注意事项
bash
undefinedbash
undefinedWorktrees don't need separate backups
Worktrees don't need separate backups
All data is in shared git repository
All data is in shared git repository
Main worktree is primary working directory
Main worktree is primary working directory
Worktrees are temporary workspaces
Worktrees are temporary workspaces
undefinedundefinedSummary
总结
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.
Git worktree可实现:
- 并行开发:同时处理多个分支
- 隔离性:保持主工作目录整洁
- 灵活性:轻松创建和移除工作区
- 有序性:结构化的worktree管理
- 高效性:快速在不同功能间切换上下文
使用worktree进行高效、隔离的多分支开发,同时保持主工作目录的整洁。