git-worktrees
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Worktrees
Git Worktrees
Second (and third, …) working directories linked to the same object store. One repo, many checkouts. Shared history, objects, refs, hooks, LFS — no re-clone.
.git第二个(以及第三个……)与同一个 对象存储关联的工作目录。一个仓库,多个检出版本。共享历史记录、对象、引用、钩子、LFS——无需重新克隆。
.gitCommands
Commands
bash
git worktree add ../hotfix hotfix-branch # existing branch
git worktree add -b new-branch ../new main # create from main
git worktree add --detach ../inspect <sha> # for read-only review
git worktree list [--porcelain]
git worktree lock ../critical # prevent prune
git worktree move ../old ../new
git worktree remove ../hotfix # after cd out
git worktree prune # clean stale metadata
git worktree repair # fix links after movesbash
git worktree add ../hotfix hotfix-branch # 现有分支
git worktree add -b new-branch ../new main # 基于main创建新分支
git worktree add --detach ../inspect <sha> # 用于只读评审
git worktree list [--porcelain]
git worktree lock ../critical # 防止被清理
git worktree move ../old ../new
git worktree remove ../hotfix # 需先退出该目录
git worktree prune # 清理过期元数据
git worktree repair # 移动后修复链接Rules
Rules
- Same branch can't be checked out in two worktrees (use or
--detach).--force - Main worktree can't be d — it owns
remove..git - on a worktree dir leaves
rm -rf— run.git/worktrees/<name>/.prune
- 同一个分支不能在两个工作树中检出(使用 或
--detach)。--force - 主工作树无法被 ——它拥有
remove目录。.git - 对工作树目录执行 会留下
rm -rf——需运行.git/worktrees/<name>/清理。prune
Workflows
Workflows
Review a PR without losing WIP:
bash
git worktree add ../review-1234 --detach origin/pr/1234Hotfix during a long build:
bash
git worktree add -b hotfix/urgent ../hotfix origin/mainPer-branch dedicated build caches: each worktree has its own , , etc. — no cross-branch invalidation.
node_modulestarget/Parallel bisect: keep main worktree for editing, run in a second.
git bisect run无需丢失进行中工作即可评审PR:
bash
git worktree add ../review-1234 --detach origin/pr/1234长构建期间处理热修复:
bash
git worktree add -b hotfix/urgent ../hotfix origin/main**按分支专用构建缓存:**每个工作树都有自己的 、 等——不会出现跨分支缓存失效的情况。
node_modulestarget/**并行二分查找:**主工作树用于编辑,在第二个工作树中运行 。
git bisect runPer-Worktree Config
Per-Worktree Config
bash
git config extensions.worktreeConfig true
git config --worktree user.email alt@example.comcore.barecore.worktreebash
git config extensions.worktreeConfig true
git config --worktree user.email alt@example.comcore.barecore.worktreePitfalls
Pitfalls
- Hooks run for every worktree — firing
post-checkoutper-worktree is correct but noisy.yarn install - Stashes are shared across worktrees (single ). Label them.
refs/stash - IDE indexers can follow in a linked worktree back to the main repo — point the IDE at the worktree root.
.git - Worktree on removable media that disappears → .
git worktree prune
- 钩子会为所有工作树运行——每个工作树触发 钩子执行
post-checkout是正确的,但会产生较多日志。yarn install - 暂存(Stashes)在所有工作树之间共享(单个 )。请为它们添加标签。
refs/stash - IDE索引器可能会跟随链接工作树中的 回到主仓库——请将IDE指向工作树根目录。
.git - 工作树所在的可移动介质消失后→运行 。
git worktree prune