worktrees
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Worktrees
Git Worktrees
Overview
概述
Git worktrees enable checking out multiple branches simultaneously in separate directories, all sharing the same repository. Create a worktree instead of stashing changes or cloning separately.
Core principle: One worktree per active branch. Switch contexts by changing directories, not branches.
Git worktree 允许在独立目录中同时检出多个分支,所有分支共享同一个仓库。无需暂存变更或单独克隆仓库,直接创建worktree即可。
核心原则: 每个活跃分支对应一个worktree。通过切换目录而非切换分支来切换上下文。
Core Concepts
核心概念
| Concept | Description |
|---|---|
| Main worktree | Original working directory from |
| Linked worktree | Additional directories created with |
Shared | All worktrees share same Git object database (no duplication) |
| Branch lock | Each branch can only be checked out in ONE worktree at a time |
| Worktree metadata | Administrative files in |
| 概念 | 描述 |
|---|---|
| 主工作目录(Main worktree) | 通过 |
| 关联工作目录(Linked worktree) | 使用 |
共享.git目录(Shared | 所有worktree共享同一个Git对象数据库(无重复存储) |
| 分支锁定(Branch lock) | 每个分支同一时间仅能在一个worktree中检出 |
| Worktree元数据 | 存储在 |
Quick Reference
速查指南
| Task | Command |
|---|---|
| Create worktree (existing branch) | |
| Create worktree (new branch) | |
| Create worktree (new branch from ref) | |
| Create detached worktree | |
| List all worktrees | |
| Remove worktree | |
| Force remove worktree | |
| Move worktree | |
| Lock worktree | |
| Unlock worktree | |
| Prune stale worktrees | |
| Repair worktree links | |
| Compare files between worktrees | |
| Get one file from another branch | |
| Get partial file changes | |
| Cherry-pick a commit | |
| Cherry-pick without committing | |
| Merge without auto-commit | |
| 任务 | 命令 |
|---|---|
| 创建worktree(基于已有分支) | |
| 创建worktree(基于新分支) | |
| 创建worktree(基于引用创建新分支) | |
| 创建分离头指针的worktree | |
| 列出所有worktree | |
| 删除worktree | |
| 强制删除worktree | |
| 移动worktree | |
| 锁定worktree | |
| 解锁worktree | |
| 清理过期worktree | |
| 修复worktree链接 | |
| 对比不同worktree的文件 | |
| 从其他分支获取单个文件 | |
| 获取文件的部分变更 | |
| 拣选提交 | |
| 拣选提交但不自动提交 | |
| 合并但不自动提交 | |
Essential Commands
核心命令
Create a Worktree
创建Worktree
bash
undefinedbash
undefinedCreate worktree with existing branch
基于已有分支创建worktree
git worktree add ../feature-x feature-x
git worktree add ../feature-x feature-x
Create worktree with new branch from current HEAD
基于当前HEAD创建新分支的worktree
git worktree add -b new-feature ../new-feature
git worktree add -b new-feature ../new-feature
Create worktree with new branch from specific commit
基于指定提交创建新分支的worktree
git worktree add -b hotfix-123 ../hotfix origin/main
git worktree add -b hotfix-123 ../hotfix origin/main
Create worktree tracking remote branch
创建跟踪远程分支的worktree
git worktree add --track -b feature ../feature origin/feature
git worktree add --track -b feature ../feature origin/feature
Create worktree with detached HEAD (for experiments)
创建分离头指针的worktree(用于实验)
git worktree add --detach ../experiment HEAD~5
undefinedgit worktree add --detach ../experiment HEAD~5
undefinedList Worktrees
列出Worktree
bash
undefinedbash
undefinedSimple list
简单列表
git worktree list
git worktree list
Verbose output with additional details
包含额外详情的详细输出
git worktree list -v
git worktree list -v
Machine-readable format (for scripting)
机器可读格式(用于脚本)
git worktree list --porcelain
**Example output:**
/home/user/project abc1234 [main]
/home/user/project-feature def5678 [feature-x]
/home/user/project-hotfix ghi9012 [hotfix-123]
undefinedgit worktree list --porcelain
**示例输出:**
/home/user/project abc1234 [main]
/home/user/project-feature def5678 [feature-x]
/home/user/project-hotfix ghi9012 [hotfix-123]
undefinedRemove a Worktree
删除Worktree
bash
undefinedbash
undefinedRemove worktree (working directory must be clean)
删除worktree(工作目录必须干净)
git worktree remove ../feature-x
git worktree remove ../feature-x
Force remove (discards uncommitted changes)
强制删除(丢弃未提交变更)
git worktree remove --force ../feature-x
undefinedgit worktree remove --force ../feature-x
undefinedMove a Worktree
移动Worktree
bash
undefinedbash
undefinedRelocate worktree to new path
将worktree迁移到新路径
git worktree move ../old-path ../new-path
undefinedgit worktree move ../old-path ../new-path
undefinedLock/Unlock Worktrees
锁定/解锁Worktree
bash
undefinedbash
undefinedLock worktree (prevents pruning if on removable storage)
锁定worktree(防止在可移动存储上被清理)
git worktree lock ../feature-x
git worktree lock --reason "On USB drive" ../feature-x
git worktree lock ../feature-x
git worktree lock --reason "On USB drive" ../feature-x
Unlock worktree
解锁worktree
git worktree unlock ../feature-x
undefinedgit worktree unlock ../feature-x
undefinedPrune Stale Worktrees
清理过期Worktree
bash
undefinedbash
undefinedRemove stale worktree metadata (after manual directory deletion)
移除过期的worktree元数据(手动删除目录后执行)
git worktree prune
git worktree prune
Dry-run to see what would be pruned
预演清理操作,查看会被清理的内容
git worktree prune --dry-run
git worktree prune --dry-run
Verbose output
详细输出
git worktree prune -v
undefinedgit worktree prune -v
undefinedRepair Worktrees
修复Worktree
bash
undefinedbash
undefinedRepair worktree links after moving directories manually
手动移动目录后修复worktree链接
git worktree repair
git worktree repair
Repair specific worktree
修复指定的worktree
git worktree repair ../feature-x
undefinedgit worktree repair ../feature-x
undefinedWorkflow Patterns
工作流模式
Pattern 1: Feature + Hotfix in Parallel
模式1:特性开发+热修复并行
To fix a bug while feature work is in progress:
bash
undefined在进行特性开发的同时修复bug:
bash
undefinedCreate worktree for hotfix from main
基于main分支创建热修复worktree
git worktree add -b hotfix-456 ../project-hotfix origin/main
git worktree add -b hotfix-456 ../project-hotfix origin/main
Switch to hotfix directory, fix, commit, push
切换到热修复目录,修复bug、提交、推送
cd ../project-hotfix
git add . && git commit -m "fix: resolve critical bug #456"
git push origin hotfix-456
cd ../project-hotfix
git add . && git commit -m "fix: resolve critical bug #456"
git push origin hotfix-456
Return to feature work
返回特性开发工作
cd ../project
cd ../project
Clean up when done
完成后清理
git worktree remove ../project-hotfix
undefinedgit worktree remove ../project-hotfix
undefinedPattern 2: PR Review While Working
模式2:开发时评审PR
To review a PR without affecting current work:
bash
undefined在不影响当前工作的前提下评审PR:
bash
undefinedFetch PR branch and create worktree
获取PR分支并创建worktree
git fetch origin pull/123/head:pr-123
git worktree add ../project-review pr-123
git fetch origin pull/123/head:pr-123
git worktree add ../project-review pr-123
Review: run tests, inspect code
评审:运行测试、检查代码
cd ../project-review
cd ../project-review
Return to work, then clean up
返回工作,然后清理
cd ../project
git worktree remove ../project-review
git branch -d pr-123
undefinedcd ../project
git worktree remove ../project-review
git branch -d pr-123
undefinedPattern 3: Compare Implementations
模式3:对比实现方案
To compare code across branches side-by-side:
bash
undefined并排对比不同分支的代码:
bash
undefinedCreate worktrees for different versions
为不同版本创建worktree
git worktree add ../project-v1 v1.0.0
git worktree add ../project-v2 v2.0.0
git worktree add ../project-v1 v1.0.0
git worktree add ../project-v2 v2.0.0
Diff, compare, or run both simultaneously
对比、比较或同时运行两个版本
diff ../project-v1/src/module.js ../project-v2/src/module.js
diff ../project-v1/src/module.js ../project-v2/src/module.js
Clean up
清理
git worktree remove ../project-v1
git worktree remove ../project-v2
undefinedgit worktree remove ../project-v1
git worktree remove ../project-v2
undefinedPattern 4: Long-Running Tasks
模式4:长时间运行任务
To run tests/builds in isolation while continuing development:
bash
undefined在隔离环境中运行测试/构建,同时继续开发:
bash
undefinedCreate worktree for CI-like testing
为类CI测试创建worktree
git worktree add ../project-test main
git worktree add ../project-test main
Start long-running tests in background
在后台启动长时间运行的测试
cd ../project-test && npm test &
cd ../project-test && npm test &
Continue development in main worktree
在主工作目录继续开发
cd ../project
undefinedcd ../project
undefinedPattern 5: Stable Reference
模式5:稳定参考分支
To maintain a clean main checkout for reference:
bash
undefined维护一个干净的main分支检出目录作为参考:
bash
undefinedCreate permanent worktree for main branch
为main分支创建永久worktree
git worktree add ../project-main main
git worktree add ../project-main main
Lock to prevent accidental removal
锁定以防止意外删除
git worktree lock --reason "Reference checkout" ../project-main
undefinedgit worktree lock --reason "Reference checkout" ../project-main
undefinedPattern 6: Selective Merging from Multiple Features
模式6:从多个特性分支选择性合并
To combine specific changes from multiple feature branches:
bash
undefined合并来自多个特性分支的特定变更:
bash
undefinedCreate worktrees for each feature to review
为每个特性分支创建worktree以便评审
git worktree add ../project-feature-1 feature-1
git worktree add ../project-feature-2 feature-2
git worktree add ../project-feature-1 feature-1
git worktree add ../project-feature-2 feature-2
Review changes in each worktree
评审每个worktree中的变更
diff ../project/src/module.js ../project-feature-1/src/module.js
diff ../project/src/module.js ../project-feature-2/src/module.js
diff ../project/src/module.js ../project-feature-1/src/module.js
diff ../project/src/module.js ../project-feature-2/src/module.js
From main worktree, selectively take changes
在主工作目录中选择性获取变更
cd ../project
git checkout feature-1 -- src/moduleA.js src/utils.js
git checkout feature-2 -- src/moduleB.js
git commit -m "feat: combine selected changes from feature branches"
cd ../project
git checkout feature-1 -- src/moduleA.js src/utils.js
git checkout feature-2 -- src/moduleB.js
git commit -m "feat: combine selected changes from feature branches"
Or cherry-pick specific commits
或拣选特定提交
git cherry-pick abc1234 # from feature-1
git cherry-pick def5678 # from feature-2
git cherry-pick abc1234 # 来自feature-1
git cherry-pick def5678 # 来自feature-2
Clean up
清理
git worktree remove ../project-feature-1
git worktree remove ../project-feature-2
undefinedgit worktree remove ../project-feature-1
git worktree remove ../project-feature-2
undefinedComparing and Merging Changes Between Worktrees
不同Worktree之间的对比与合并
Since all worktrees share the same Git repository, you can compare files, cherry-pick commits, and selectively merge changes between them.
由于所有worktree共享同一个Git仓库,你可以在它们之间对比文件、拣选提交以及选择性合并变更。
Compare and Review File Changes
对比与评审文件变更
Since worktrees are just directories, you can compare files directly:
bash
undefinedworktree本质上是目录,因此可以直接对比文件:
bash
undefinedCompare specific file between worktrees
对比不同worktree中的特定文件
diff ../project-main/src/app.js ../project-feature/src/app.js
diff ../project-main/src/app.js ../project-feature/src/app.js
Use git diff to compare branches (works from any worktree)
使用git diff对比分支(可在任意worktree中执行)
git diff main..feature-branch -- src/app.js
git diff main..feature-branch -- src/app.js
Visual diff with your preferred tool
使用你偏好的工具进行可视化对比
code --diff ../project-main/src/app.js ../project-feature/src/app.js
code --diff ../project-main/src/app.js ../project-feature/src/app.js
Compare entire directories
对比整个目录
diff -r ../project-v1/src ../project-v2/src
undefineddiff -r ../project-v1/src ../project-v2/src
undefinedMerge Only One File from a Worktree
仅合并单个文件来自某个Worktree
You can selectively bring a single file from another branch using :
git checkoutbash
undefined你可以使用选择性地从其他分支获取单个文件:
git checkoutbash
undefinedIn your current branch, get a specific file from another branch
在当前分支中,从其他分支获取特定文件
git checkout feature-branch -- path/to/file.js
git checkout feature-branch -- path/to/file.js
Or get it from a specific commit
或从特定提交获取
git checkout abc1234 -- path/to/file.js
git checkout abc1234 -- path/to/file.js
Get multiple specific files
获取多个特定文件
git checkout feature-branch -- src/module.js src/utils.js
For **partial file changes** (specific hunks/lines only):
```bashgit checkout feature-branch -- src/module.js src/utils.js
对于**部分文件变更**(仅特定代码块/行):
```bashInteractive patch mode - select which changes to take
交互式补丁模式 - 选择要获取的变更
git checkout -p feature-branch -- path/to/file.js
This prompts you to accept/reject each change hunk individually with options:
- `y` - apply this hunk
- `n` - skip this hunk
- `s` - split into smaller hunks
- `e` - manually edit the hunkgit checkout -p feature-branch -- path/to/file.js
这会提示你逐个接受/拒绝每个变更块,选项包括:
- `y` - 应用此变更块
- `n` - 跳过此变更块
- `s` - 拆分为更小的变更块
- `e` - 手动编辑变更块Cherry-Pick Commits from Worktrees
从Worktree拣选提交
Cherry-picking works at the commit level. Since all worktrees share the same repository, you can cherry-pick any commit:
bash
undefined拣选操作基于提交级别。由于所有worktree共享同一个仓库,你可以拣选任意提交:
bash
undefinedFind the commit hash (from any worktree or git log)
查找提交哈希(可在任意worktree或git log中查看)
git log feature-branch --oneline
git log feature-branch --oneline
Cherry-pick specific commit into your current branch
将特定提交拣选到当前分支
git cherry-pick abc1234
git cherry-pick abc1234
Cherry-pick multiple commits
拣选多个提交
git cherry-pick abc1234 def5678
git cherry-pick abc1234 def5678
Cherry-pick a range of commits
拣选一个范围内的提交
git cherry-pick abc1234^..def5678
git cherry-pick abc1234^..def5678
Cherry-pick without committing (stage changes only)
拣选提交但不自动提交(仅暂存变更)
git cherry-pick --no-commit abc1234
undefinedgit cherry-pick --no-commit abc1234
undefinedMerge Changes from Multiple Worktrees
从多个Worktree合并变更
You can merge or cherry-pick from multiple branches:
bash
undefined你可以依次合并多个分支,或同时合并:
bash
undefinedMerge multiple branches sequentially
依次合并多个分支
git merge feature-1
git merge feature-2
git merge feature-1
git merge feature-2
Or use octopus merge for multiple branches at once
或使用章鱼合并同时合并多个分支
git merge feature-1 feature-2 feature-3
git merge feature-1 feature-2 feature-3
Cherry-pick commits from multiple branches
从多个分支拣选提交
git cherry-pick abc1234 # from feature-1
git cherry-pick def5678 # from feature-2
undefinedgit cherry-pick abc1234 # 来自feature-1
git cherry-pick def5678 # 来自feature-2
undefinedSelective Merging - Pick Which Changes to Include
选择性合并 - 选择要包含的变更
Option 1: Selective File Checkout
选项1:选择性文件检出
bash
undefinedbash
undefinedGet specific files from different branches
从不同分支获取特定文件
git checkout feature-1 -- src/moduleA.js
git checkout feature-2 -- src/moduleB.js
git commit -m "Merge selected files from feature branches"
undefinedgit checkout feature-1 -- src/moduleA.js
git checkout feature-2 -- src/moduleB.js
git commit -m "Merge selected files from feature branches"
undefinedOption 2: Interactive Patch Selection
选项2:交互式补丁选择
bash
undefinedbash
undefinedSelect specific hunks from a file
选择文件中的特定代码块
git checkout -p feature-1 -- src/shared.js
undefinedgit checkout -p feature-1 -- src/shared.js
undefinedOption 3: Cherry-Pick with Selective Staging
选项3:拣选并选择性暂存
bash
undefinedbash
undefinedApply changes without committing
应用变更但不提交
git cherry-pick --no-commit abc1234
git cherry-pick --no-commit abc1234
Unstage what you don't want
取消暂存不需要的文件
git reset HEAD -- unwanted-file.js
git checkout -- unwanted-file.js
git reset HEAD -- unwanted-file.js
git checkout -- unwanted-file.js
Commit only what you kept
仅提交保留的变更
git commit -m "Selected changes from feature-1"
undefinedgit commit -m "Selected changes from feature-1"
undefinedOption 4: Merge with Manual Selection
选项4:合并并手动选择
bash
undefinedbash
undefinedStart merge but don't auto-commit
开始合并但不自动提交
git merge --no-commit feature-1
git merge --no-commit feature-1
Review and modify staged changes
评审并修改暂存的变更
git status
git reset HEAD -- file-to-exclude.js
git checkout -- file-to-exclude.js
git status
git reset HEAD -- file-to-exclude.js
git checkout -- file-to-exclude.js
Commit your selection
提交你的选择
git commit -m "Merge selected changes from feature-1"
undefinedgit commit -m "Merge selected changes from feature-1"
undefinedOption 5: Using git restore (Git 2.23+)
选项5:使用git restore(Git 2.23+)
bash
undefinedbash
undefinedRestore specific file from another branch
从其他分支恢复特定文件
git restore --source=feature-branch -- path/to/file.js
git restore --source=feature-branch -- path/to/file.js
Interactive restore with patch selection
交互式恢复并选择补丁
git restore -p --source=feature-branch -- path/to/file.js
undefinedgit restore -p --source=feature-branch -- path/to/file.js
undefinedDirectory Structure Conventions
目录结构规范
Organize worktrees predictably:
~/projects/
myproject/ # Main worktree (main/master branch)
myproject-feature-x/ # Feature branch worktree
myproject-hotfix/ # Hotfix worktree
myproject-review/ # Temporary PR review worktreeNaming convention: or
<project>-<purpose><project>-<branch>可预测地组织worktree:
~/projects/
myproject/ # 主工作目录(main/master分支)
myproject-feature-x/ # 特性分支worktree
myproject-hotfix/ # 热修复worktree
myproject-review/ # 临时PR评审worktree命名规范: 或
<项目名>-<用途><项目名>-<分支名>Best Practices
最佳实践
| Practice | Rationale |
|---|---|
| Use sibling directories | Keep worktrees at same level as main project for easy navigation |
| Name by purpose | |
| Clean up promptly | Remove worktrees when done to avoid confusion |
| Lock remote worktrees | Prevent pruning if worktree is on network/USB storage |
Use | Avoid creating throwaway branches |
| Commit before removing | Always commit or stash before |
| 实践 | 理由 |
|---|---|
| 使用同级目录 | 将worktree与主项目放在同一层级,便于导航 |
| 按用途命名 | |
| 及时清理 | 完成任务后立即删除worktree,避免混乱 |
| 锁定远程worktree | 若worktree位于网络/USB存储,锁定以防止被清理 |
实验时使用 | 避免创建临时分支 |
| 删除前提交变更 | 执行 |
Common Issues and Solutions
常见问题与解决方案
Issue: "Branch is already checked out"
问题:"Branch is already checked out"
Cause: Attempting to checkout a branch that's active in another worktree.
Solution:
bash
undefined原因: 尝试检出已在其他worktree中激活的分支。
解决方案:
bash
undefinedFind where the branch is checked out
查找分支所在的worktree
git worktree list
git worktree list
Either work in that worktree or remove it first
要么在该worktree中操作,要么先删除它
git worktree remove ../other-worktree
undefinedgit worktree remove ../other-worktree
undefinedIssue: Stale worktree after manual deletion
问题:手动删除目录后出现过期worktree
Cause: Deleted worktree directory without using .
git worktree removeSolution:
bash
undefined原因: 未使用就删除了worktree目录。
git worktree remove解决方案:
bash
undefinedClean up stale metadata
清理过期的元数据
git worktree prune
undefinedgit worktree prune
undefinedIssue: Worktree moved manually
问题:手动移动了worktree
Cause: Moved worktree directory without using .
git worktree moveSolution:
bash
undefined原因: 未使用就移动了worktree目录。
git worktree move解决方案:
bash
undefinedRepair the worktree links
修复worktree链接
git worktree repair
git worktree repair
Or specify the new path
或指定新路径
git worktree repair /new/path/to/worktree
undefinedgit worktree repair /new/path/to/worktree
undefinedIssue: Worktree on removed drive
问题:worktree所在存储设备已移除
Cause: Worktree was on removable storage that's no longer connected.
Solution:
bash
undefined原因: worktree位于已断开连接的可移动存储上。
解决方案:
bash
undefinedIf temporary, lock it to prevent pruning
若为临时情况,锁定以防止被清理
git worktree lock ../usb-worktree
git worktree lock ../usb-worktree
If permanent, prune it
若为永久移除,清理它
git worktree prune
undefinedgit worktree prune
undefinedCommon Mistakes
常见错误
| Mistake | Fix |
|---|---|
Using | Always use |
| Forgetting branch is locked to worktree | Run |
| Not cleaning up temporary worktrees | Remove worktrees immediately after task completion |
| Creating worktrees in nested locations | Use sibling directories ( |
| Moving worktree directory manually | Use |
| 错误 | 修复方法 |
|---|---|
使用 | 始终使用 |
| 忘记分支已被worktree锁定 | 出现检出错误前先运行 |
| 未清理临时worktree | 任务完成后立即删除worktree |
| 在嵌套位置创建worktree | 使用同级目录( |
| 手动移动worktree目录 | 使用 |
Agent Workflow Integration
Agent工作流集成
To isolate parallel agent tasks:
bash
undefined隔离并行Agent任务:
bash
undefinedCreate worktree for isolated task
为隔离任务创建worktree
git worktree add -b task-123 ../project-task-123
cd ../project-task-123
git worktree add -b task-123 ../project-task-123
cd ../project-task-123
Make changes, run tests, return
进行变更、运行测试,然后返回
cd ../project
To experiment safely with detached HEAD:
```bashcd ../project
安全地使用分离头指针进行实验:
```bashCreate detached worktree (no branch to clean up)
创建分离头指针的worktree(无需清理临时分支)
git worktree add --detach ../project-experiment
cd ../project-experiment
git worktree add --detach ../project-experiment
cd ../project-experiment
Experiment, then discard or commit to new branch
进行实验,然后丢弃或提交到新分支
git worktree remove --force ../project-experiment
undefinedgit worktree remove --force ../project-experiment
undefinedVerification Checklist
验证清单
Before using worktrees:
- Understand that branches can only be checked out in one worktree
- Know where worktrees will be created (use sibling directories)
- Plan cleanup strategy for temporary worktrees
When creating worktrees:
- Use descriptive directory names
- Verify branch is not already checked out elsewhere
- Consider using for experiments
--detach
When removing worktrees:
- Commit or stash any uncommitted changes
- Use , not
git worktree removerm -rf - Run if directory was deleted manually
git worktree prune
使用worktree前:
- 理解分支同一时间仅能在一个worktree中检出
- 确定worktree的创建位置(使用同级目录)
- 规划临时worktree的清理策略
创建worktree时:
- 使用描述性目录名
- 验证分支未在其他位置检出
- 实验时考虑使用
--detach
删除worktree时:
- 提交或暂存所有未提交变更
- 使用而非
git worktree removerm -rf - 若手动删除了目录,运行
git worktree prune