git worktree
git
用法
git worktree add <path> [<branch>]從同一個儲存庫再簽出一個工作目錄,不用重新複製就能同時開兩個分支;同一個分支不能被簽出兩次,手動刪掉目錄後項目要等 prune 才清掉。
常用選項
| 選項 | 含義 |
|---|---|
| add <path> <branch> | check that branch out into another folder |
| add -b <new> <path> | create a branch and check it out there |
| list | show every working tree of this repository |
| remove <path> | delete a working tree cleanly |
| prune | forget trees whose folder you deleted by hand |
| --detach | check out a commit without a branch |
範例
git worktree add ../hotfix hotfix/logina second folder on another branch, sharing one .git
git worktree add -b release ../rel maincreates release from main in ../rel
git worktree remove ../hotfixremoves the folder and its bookkeeping entry
難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. git worktree 是做什麼的?
從同一個儲存庫再簽出一個工作目錄,不用重新複製就能同時開兩個分支;同一個分支不能被簽出兩次,手動刪掉目錄後項目要等 prune 才清掉。
Q. 怎麼寫?
git worktree add <path> [<branch>] —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 6 個,完整列表在 man git。這條命令屬於git。
相關命令
man 手冊: man git-worktree