Git Worktree 实战:多分支并行开发
使用 git worktree 同时在多个分支上工作,提高开发效率。
Git Worktree 是被低估的强大功能,允许同时检出多个分支。
基本用法
# 创建新的 worktree
git worktree add ../feature-login feature/login
# 列出所有 worktrees
git worktree list
# 移除 worktree
git worktree remove ../feature-login
实际场景
当你正在开发功能 A,突然需要修复 bug:
# 保存当前工作
git stash
# 创建 worktree 修复 bug
git worktree add ../hotfix master
cd ../hotfix
# 修复 bug...
git commit -m "fix: urgent bug fix"
git push
# 回到原分支继续工作
git worktree remove ../hotfix
git checkout feature/a
git stash pop
注意事项
- 避免在同一个仓库创建过多 worktree
- 确保及时清理已完成的 worktree
Worktree 让多任务切换变得优雅。