用 git worktree 并行处理分支

无需暂存或重新克隆即可同时处理两个分支。

macOSGit命令行效率低风险
运行 git worktree add ../hotfix hotfix,为该分支创建关联检出。

01命令

命令
$ git worktree add ../hotfix hotfix

02作用说明

关联工作树与主克隆共享对象库和引用,因此可以在独立目录中构建和测试热修或审阅分支,而不影响当前检出。避免重复克隆大仓库,节省时间和磁盘。

03风险

风险低风险
使用条件Git 2.20+ (install with Homebrew or Xcode Command Line Tools)
系统兼容性macOS with Git installed (Homebrew or Xcode Command Line Tools)
!注意: 手工删除工作树目录会残留元数据;请使用 git worktree remove。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • git-worktree documentation
    Git · vendor-doc · 2026-09-20
    依据: Documents git worktree, which attaches multiple working trees to one repository.

最后验证: 2026-10-13 · 分类: 开发者

06相关方案