<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Git 工作流進化 - Bobo 的學思山丘</title><description>Git 工作流演進系列：從 stash 到 worktree，從 tig 到 lazygit</description><link>https://bobochen.dev/</link><item><title>Git Worktree 入門 — 不用 stash 的多分支並行開發</title><link>https://bobochen.dev/blog/git-worktree-beginner-guide/</link><guid isPermaLink="true">https://bobochen.dev/blog/git-worktree-beginner-guide/</guid><description>還在用 git stash 切分支嗎？Git Worktree 讓你同時 checkout 多個分支到不同目錄，徹底消除 stash 的認知負擔。</description><pubDate>Mon, 09 Mar 2026 00:00:00 GMT</pubDate><content:encoded>## 背景

你有沒有遇過這種情況：正在改一個 feature 改到一半，PM 突然說線上有 bug 要修。

傳統做法通常是：

1. `git stash` 暫存手上的改動
2. `git checkout hotfix-branch`
3. 修完 bug，commit，push
4. `git checkout feature-branch`
5. `git stash pop` 把改動拿回來
6. 然後發現 stash 衝突了...

或者更暴力一點：直接 commit 一個 `WIP: 先存一下` 的 commit，之後再 squash。這兩種做法都不理想 — stash 容易忘、WIP commit 污染歷史。

## 什麼是 Git Worktree

Git worktree 讓你把同一個 repo 的不同分支，同時 checkout 到不同的目錄。

```bash
# 你的主要工作目錄
~/projects/my-app/          ← main branch

# 建立一個 worktree 來修 hotfix
git worktree add ../my-app-hotfix hotfix/urgent-fix

# 現在你有兩個目錄，各自獨立
~/projects/my-app/          ← 繼續改 feature（不用 stash）
~/projects/my-app-hotfix/   ← 修 hotfix
```

重點：**兩個目錄共享同一個 `.git`**。commit history、remote、config 都是同一份。但工作目錄是獨立的，不會互相干擾。

## 基本操作

### 建立 worktree

```bash
# 從既有分支建立
git worktree add ../my-app-hotfix hotfix/urgent-fix

# 建立新分支 + worktree（一步到位）
git worktree add -b feature/new-thing ../my-app-new-feature
```

### 列出所有 worktree

```bash
git worktree list
# /Users/bobo/projects/my-app              abc1234 [main]
# /Users/bobo/projects/my-app-hotfix       def5678 [hotfix/urgent-fix]
```

### 刪除 worktree

```bash
# 先刪目錄，再清理
rm -rf ../my-app-hotfix
git worktree prune

# 或一步搞定
git worktree remove ../my-app-hotfix
```

## 什麼時候該用 Worktree

| 情境                         | 傳統做法               | Worktree 做法               |
| ---------------------------- | ---------------------- | --------------------------- |
| Feature 改到一半要修 hotfix  | stash → checkout → pop | 開新 worktree，直接修       |
| 同時跑兩個分支的測試         | 切來切去               | 兩個目錄各跑各的            |
| Code review 時想看完整的分支 | checkout 過去看        | 開 worktree，用編輯器直接開 |
| 比較兩個分支的行為差異       | 切來切去或用 diff      | 兩個目錄並排開              |

## 注意事項

1. **同一個分支不能同時 checkout 到兩個 worktree** — Git 會阻止你
2. **node_modules 不共享** — 每個 worktree 要各自 `npm install`（但這也是優點，環境完全獨立）
3. **命名慣例** — 建議 worktree 目錄放在主 repo 旁邊，用 `{repo}-{用途}` 命名

## 反思

### 技術面

Worktree 從 Git 2.5（2015 年）就有了，但很多人不知道。原因很簡單：大多數 Git 教學不會教它，因為它不是「基礎操作」。但一旦你的工作流需要頻繁切換分支，它的價值就顯現出來了。

### 心態面

stash 用久了會覺得「就是這樣嘛」，但其實每次 stash 都有認知負擔 — 我 stash 了什麼？有幾個 stash？pop 會不會衝突？Worktree 的做法是從根本上消除這個問題：不需要暫存，因為你根本不需要離開。

### 有趣發現

很多 AI coding 工具（像 Claude Code）也開始支援 worktree 模式，讓 AI 在獨立的 worktree 裡工作，不影響你手上正在改的程式碼。這個概念和人類開發者用 worktree 的理由完全一樣 — 隔離。</content:encoded><media:content url="https://bobochen.dev/_astro/cover.bIvLkike.webp" medium="image"/><category>Git</category><category>Git Worktree</category><category>開發工具</category><category>CLI</category><category>版本控制</category><enclosure url="https://bobochen.dev/_astro/cover.bIvLkike.webp" length="0" type="image/png"/></item></channel></rss>