1. 什么是 git worktree? #

2. 前置知识 #

2.1 仓库、工作目录、分支 #

2.2 普通切换分支 vs worktree #

2.3 主工作树与两个概念 #

3. 为什么需要 worktree? #

3.1 功能做到一半,突然要修 bug #

3.2 需要同时看两个分支的代码 #

3.3 和「再 clone 一次」比有什么好处? #

4. 环境准备 #

4.1 检查 Git 版本 #

:: 说明:查看当前安装的 Git 版本号
git --version

4.2 一键创建仓库 #

:: 说明:若已存在演示目录则先删除,保证可重复运行
if exist wt-repo rd /s /q wt-repo

:: 说明:新建仓库目录
md wt-repo

:: 说明:进入仓库
cd wt-repo

:: 说明:初始化 Git 仓库(默认分支名设为 main)
git init -b main

:: 说明:配置本仓库专用的用户名(仅影响当前仓库)
git config user.name "Worktree"

:: 说明:配置本仓库专用的邮箱
git config user.email "Worktree@example.com"

:: 说明:写入一个示例文件
echo hello worktree> README.md

:: 说明:把文件加入暂存区
git add README.md

:: 说明:创建第一次提交
git commit -m "init: repo"

:: 说明:打印当前目录与当前分支,确认创建成功
cd
git branch --show-current

5. 核心概念 #

5.1 每个分支同一时刻只能在一个工作树里 #

5.2 新目录建议放在主仓库旁边 #

5.3 删目录不等于彻底清理 #

6. 常用命令 #

6.1 查看所有工作树 #

:: 说明:进入仓库(若你已在该目录可省略)
cd wt-repo

:: 说明:列出当前仓库关联的全部工作树
git worktree list

6.2 添加工作树(检出已有分支) #

:: 说明:进入主仓库
cd wt-repo

:: 说明:从当前 HEAD 创建分支 feature/notes(还不打开新工作树)
git branch feature/notes

:: 说明:把 feature/notes 检出到上一级目录的 wt-repo-notes 文件夹
git worktree add ..\wt-repo-notes feature/notes

:: 说明:再次列出工作树,应看到两行
git worktree list

6.3 添加工作树(同时新建分支) #

:: 说明:进入主仓库
cd wt-repo

:: 说明:基于 main 新建 hotfix/urgent,并检出到旁边的 wt-repo-hotfix
git worktree add -b hotfix/urgent ..\wt-repo-hotfix main

:: 说明:进入新工作树看当前分支
cd ..\wt-repo-hotfix
git branch --show-current

:: 说明:回到主仓库(主仓库仍保持原来的状态)
cd ..\wt-repo
git branch --show-current

6.4 移除工作树 #

:: 说明:确保当前不在即将删除的工作树里,回到主仓库
cd wt-repo

:: 说明:移除 notes 工作树(会删除对应目录)
git worktree remove ..\wt-repo-notes

:: 说明:移除 hotfix 工作树
git worktree remove ..\wt-repo-hotfix

:: 说明:查看结果,应只剩主工作树
git worktree list

6.5 手动删目录后的清理 #

:: 说明:进入主仓库
cd wt-repo

:: 说明:清理已不存在目录的残留 worktree 记录
git worktree prune

:: 说明:再次查看列表确认
git worktree list

7. 实战:功能做到一半,紧急修 bug #

:: 说明:清理上次演示留下的目录
if exist wt-repo rd /s /q wt-repo
if exist wt-repo-hotfix rd /s /q wt-repo-hotfix

:: 说明:创建并进入主仓库
md wt-repo
cd wt-repo
git init -b main
git config user.name "Worktree Demo"
git config user.email "demo@example.com"
echo version on main> README.md
git add README.md
git commit -m "init: main"

:: 说明:模拟你在写功能:新建 feature 分支并留下未提交修改
git switch -c feature/login
echo WIP: login form half done> feature.txt

:: 说明:此时有未提交文件,但我们不去 stash,而是另开 hotfix 工作树
git status --short

:: 说明:基于 main 开 hotfix 工作树(不碰主仓库未完成改动)
git worktree add -b hotfix/urgent ..\wt-repo-hotfix main

:: 说明:进入 hotfix 目录,修复一个假 bug
cd ..\wt-repo-hotfix
echo version on main - bug fixed> README.md
git add README.md
git commit -m "fix: critical bug on main"

:: 说明:打印 hotfix 提交结果
echo hotfix 最新提交:
git log -1 --oneline

:: 说明:回到 feature 目录,未完成改动仍然在
cd ..\wt-repo
echo 主仓库仍有未提交文件:
git status --short
echo 当前分支:
git branch --show-current

:: 说明:清理本次演示
git worktree remove ..\wt-repo-hotfix
cd ..
rd /s /q wt-repo
echo 演示完成并已清理

8. 实战:同时查看两个分支 #

:: 说明:清理旧演示目录
if exist wt-repo rd /s /q wt-repo
if exist wt-repo-ui rd /s /q wt-repo-ui

:: 说明:创建主仓库并提交初始内容
md wt-repo
cd wt-repo
git init -b main
git config user.name "Worktree Demo"
git config user.email "demo@example.com"
echo console.log('main');> app.js
git add app.js
git commit -m "init: main app"

:: 说明:创建 feature/ui 分支并改文件,再切回 main
git switch -c feature/ui
echo console.log('feature ui');> app.js
git add app.js
git commit -m "feat: ui branch change"
git switch main

:: 说明:把 feature/ui 放到旁边目录,主目录保持 main
git worktree add ..\wt-repo-ui feature/ui

:: 说明:对比两个目录中同一文件的内容
echo ===== main 目录 app.js =====
type app.js
echo ===== ui 工作树 app.js =====
type ..\wt-repo-ui\app.js

:: 说明:用 git 看两个分支差异(在主仓库执行即可)
git diff main..feature/ui

:: 说明:清理
git worktree remove ..\wt-repo-ui
cd ..
rd /s /q wt-repo
echo 演示完成并已清理

9. 常见问题 #

9.1 提示分支已被检出(already checked out) #

9.2 remove 失败,提示有未提交修改 #

9.3 list 里还能看到已删除的目录 #

9.4 主仓库还能看到 hotfix 分支吗? #

10. 命令速查 #

操作 命令
进入目录 cd wt-repo
新建目录 md wt-repo
删除目录 rd /s /q wt-repo
查看工作树 git worktree list
基于现有分支新建目录 git worktree add ..\项目-notes feature/notes
新建分支并打开目录 git worktree add -b hotfix/urgent ..\项目-hotfix main
移除工作树 git worktree remove ..\项目-hotfix
强制移除 git worktree remove --force ..\项目-hotfix
清理失效记录 git worktree prune