Git 2.5 之後 新增了 worktree 指令
使用 worktree
會在 當前目錄下 產生一個 子資料夾 並且該子資料夾 checkout 至給定的 branch 或是 ref
在該子資料夾的操作 就彷彿 你在令一個 Git 根專案目錄下操作
你可以 commit, push, pull 當你不需要這一個子資料夾時 直接將它刪除即可
使用情境
你目前正在 develop 這條 branch 開發 但你需要切換到 master 上看 code 或者是你需要切換到 master 上修改 code
$ git worktree add master-folder master
# 在 git 專案根目錄下 建立一個 master-folder 資料夾 並切到 master
關於 worktree 相關的資料會被存在 .git/worktree 一陣子過後 它就會被去除掉 但你可以透過以下的指令清除掉 worktree 相關的資料
$ git worktree prune
Reference
Six cool features of the Git 2.x series