Heidi Labs
GitHub TrendingGitHub Trending 项目约 9 分钟3,543 字

worktrunk:让 git worktree 像分支一样简单

本文目录

2026 年 11 月:Worktrunk 在今年年初发布,并迅速成为最受欢迎的 git worktree 管理器。它用爱构建(没有粗制滥造!)。请告诉我任何一点摩擦——我正全力专注于让 Worktrunk 继续出色,而最大的帮助就是人们指出他们感受到的问题。

Worktrunk 是一个用于 git worktree 管理的命令行工具(CLI),专为并行运行 AI 代理而设计。

Worktrunk 的三个核心命令让 worktree 像分支一样简单。此外,Worktrunk 还有一系列提升体验的功能,用于简化大量并行改动的工作,包括自动化本地工作流的钩子(hooks)和写时复制(copy-on-write)构建缓存。

一个快速演示:

📚 完整文档见 worktrunk.dev 📚

背景:git worktree#

像 Claude Code 和 Codex 这样的 AI 代理可以在无人监督的情况下处理更长的任务,因此同时管理 5-10 个以上是可能的。Git 原生的 worktree 功能让每个代理拥有自己的工作目录,这样它们就不会互相干扰彼此的改动。

但 git worktree 的用户体验很笨拙。即便是启动一个新 worktree 这样的小任务,也需要把分支名输入三次:git worktree add -b feat ../repo.feat,然后 cd ../repo.feat。

Worktrunk 让 git worktree 像分支一样简单#

Worktree 通过分支名来寻址;路径由可配置的模板计算得出。接受分支的命令也接受其检出所在 worktree 的路径。

从核心命令开始#

核心命令:

任务Worktrunk原生 git
切换 worktreewt switch featcd ../repo.feat
创建并启动 Claudewt switch -c -x claude featgit worktree add -b feat ../repo.feat &&<br>cd ../repo.feat &&<br>claude
清理wt removecd ../repo &&<br>git worktree remove ../repo.feat &&<br>git branch -d feat
带状态列出wt listgit worktree list(仅路径)

按需扩展到更高级的命令#

工作流自动化:

  • 钩子(Hooks)——在创建、合并前、合并后等时机运行命令
  • LLM 提交信息——从 diff 生成提交信息
  • 合并工作流——一条命令完成 squash、rebase、merge、清理
  • 交互式选择器——浏览 worktree,实时查看 CI 状态和 diff、日志、PR 与评论预览
  • 共享构建缓存——十个 worktree 共用 target/、node_modules/ 等,无需构建或复制(在 APFS、btrfs 和 XFS 上)
  • wt list --full——每个分支的 CI 状态和 AI 生成的摘要
  • PR 检出——wt switch pr:123 直接跳到某个 PR 的分支
  • 每个 worktree 一个开发服务器——hash_port 模板过滤器为每个 worktree 分配唯一端口
  • 别名与按分支变量——自定义 wt <name> 命令,以及用于钩子模板的分支作用域状态
  • ……还有很多

多个并行代理,同样简单的命令:

安装#

Homebrew(macOS 与 Linux):

brew install worktrunk && wt config shell install

Shell 集成允许命令切换目录。

Cargo:

cargo install worktrunk && wt config shell install

Windows 及其他#

Windows。wt 默认使用 Windows Terminal 的命令,因此 Winget 会额外把 Worktrunk 安装为 git-wt 以避免冲突:

winget install max-sixty.worktrunk
git-wt config shell install

或者,禁用 Windows Terminal 的别名(设置 → 应用 → 高级应用设置 → 应用执行别名 → "Terminal"/"Terminal Preview"),以直接使用 wt。

免费代码签名由 SignPath.io 提供,证书由 SignPath Foundation 提供——政策。

Arch Linux:

sudo pacman -S worktrunk && wt config shell install

Conda / Pixi(社区维护的 feedstock):

conda install -c conda-forge worktrunk && wt config shell install

或用 Pixi:pixi global install worktrunk && wt config shell install。

快速开始#

为一个新功能创建 worktree:

$ wt switch --create feature-auth
✓ Created branch feature-auth from main and worktree @ ~/repo.feature-auth

这会创建一个新分支和 worktree,然后切换到它。完成工作后,用 wt list 检查所有 worktree:

$ wt list
 Branch       Status  HEAD±    main↕    main…±   Remote⇅  Commit   Age   Message
@ feature-auth   +     ↑        +27 -8    ↑1      +31      4bc72dc  2h    Add authenticatio…
^ main                 ^⇡       ⇡1                          0e631ad  1d    Initial commit

○ Showing 2 worktrees, 1 with changes, 1 ahead, hidden: Path

@ 标记当前 worktree。+ 表示已暂存的改动,↑1 表示领先 main 1 个提交,⇡ 表示尚未推送的提交。

完成后,可以:

PR 工作流——提交、推送、开 PR,通过 GitHub/GitLab 合并,然后清理:

wt step commit      # 提交已暂存的改动
gh pr create        # 或 glab mr create
wt remove           # PR 合并后

本地合并——squash、rebase 到 main、快进合并、清理:

$ wt merge main
◎ Generating commit message and committing changes... (2 files, +53, no squashing needed)
 Add authentication module
✓ Committed changes @ a1b2c3d
◎ Merging 1 commit to main @ a1b2c3d (no rebase needed)
 * a1b2c3d Add authentication module
 auth.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib.rs  |  2 ++
 2 files changed, 53 insertions(+)
✓ Merged to main (1 commit, 2 files, +53)
◎ Removing feature-auth worktree & branch in background (same commit as main, _)
○ Switched to worktree for main @ ~/repo

对于并行代理,创建多个 worktree 并在每个里面启动一个代理:

wt switch -x claude -c feature-a -- 'Add user authentication'
wt switch -x claude -c feature-b -- 'Fix the pagination bug'
wt switch -x claude -c feature-c -- 'Write tests for the API'

-x 标志在切换后运行一个命令;-- 之后的参数会传给它。配置启动后钩子(post-start hooks)来自动化设置(安装依赖、启动开发服务器)。

下一步#

  • 学习核心命令:wt switch、wt list、wt merge、wt remove
  • 设置钩子以自动完成初始化
  • 探索 LLM 提交信息、交互式选择器、Claude Code 集成、CI 状态与 PR 链接
  • 浏览技巧与模式,获取配方:别名、开发服务器、数据库、代理交接等
  • 扩展 Worktrunk——用钩子和别名自定义工作流
  • 观看 @DevOpsToolbox 关于 Worktrunk 的视频
  • 运行 wt --help 或 wt <command> --help 快速查阅 CLI

贡献#

⭐ 给仓库点星

告诉朋友 Worktrunk

  • 提交 issue——反馈、功能请求,哪怕是很小的摩擦或不完美的用户提示,或者尚未解决的 worktree 痛点
  • 分享:X · Reddit · LinkedIn

📚 完整文档见 worktrunk.dev 📚