Git Worktree
適用場景:需要暫時切換處理 hotfix,但不想 stash 當前工作。
確認目前位置
git branch
# feature/l-001從 main 建立 hotfix worktree
git worktree add -b hotfix-123 .worktrees/hotfix-123 main或:
git worktree add .worktrees/hotfix-123 maingit worktree list
/workspace/git-playground 0e439b8 [feature/l-001]
/workspace/git-playground/.worktrees/hotfix-123 0e439b8 [main]切換到 hotfix 目錄
cd .worktrees/hotfix-123如果使用第二種方式,需要額外 checkout:
git checkout -b hotfix-123在 hotfix branch 修復問題
# 修改檔案
git add .
git commit -m "fix: issue description"推送並合併 hotfix
git push origin hotfix-123
# 在遠端完成 PR & Merge 到 main切回專案根目錄
cd ../..清理 hotfix worktree
git worktree remove .worktrees/hotfix-123
git fetch
git pull origin main繼續原本 feature branch 的開發工作
Demo
git worktree add ../worktree/feature/ABC-12345 prod.1.112.4 -b feature/ABC-12345