- Published on
git worktree
- Authors
- Name
之前就聽說 git worktree 很好用,有稍微學過,但一直沒有常用,直到再次被 threads 的貼文打到,還有開始用 claude code 想要更多工處理事情,特此記錄一下 git work tree 的使用方式。
git worktree 介紹
git worktree 用來讓同一個 repo 同時 checkout 多個分支到不同的目錄。這對於以下情境特別有用:
想在同一個專案的不同分支上同時開發(例如:修 bug 用 main 分支,開發新功能用 feature 分支)
想要避免在切換分支時因為工作目錄未提交而遇到阻礙
想在 CI/CD 或腳本中快速處理多個版本的 code
基本概念
通常,一個 Git repo 只有一個工作目錄(working directory),只能檢出一個分支。但透過 git worktree,你可以:
在不同目錄中 checkout 不同分支(或 commit)
共享相同的 .git 物件(節省空間)
常見使用方式
- 建立新的 worktree
git worktree add ../feature-branch feature-branch
../feature-branch:新目錄位置feature-branch:要檢出的 branch
- branch 不存在
git worktree add -b new-branch ../new-branch origin/main
這會在 ../new-branch 建立一個新目錄,並從 origin/main 開新分支 new-branch。
- 查看已存在的 worktree
git worktree list
會列出所有 worktree 的路徑與 branch 狀態。
- 移除 worktree
git worktree remove ../feature-branch
會移除 worktree 目錄與其相關設定(不會刪掉 branch 本身)。
- 清理已刪除目錄的 worktree 記錄
如果你手動刪了 worktree 目錄,需要清理:
git worktree prune
移除目錄與對應的 worktree 記錄(不會刪除分支)
⚠️ 注意事項
同一個分支不能在多個 worktree 同時 checkout
worktree 不建議放在原 repo 的子目錄中(可能造成混亂)
建議使用 ../ 開頭放在 repo 外層目錄