第4课:分支管理与协作
课程目标
- 理解Worktree与分支管理的关系
- 掌握跨Worktree的协作模式
- 学会在团队开发中使用Worktree
理论讲解
Worktree与分支管理的关系
Git Worktree与分支管理密切相关,但又有明显的区别:
- 分支是逻辑概念:分支是代码的不同版本线
- Worktree是物理概念:Worktree是分支在文件系统中的具体体现
- 一对一关系:一个Worktree对应一个分支(或commit)
- 多对一关系:一个分支可以有多个Worktree(使用--detach选项)
Worktree中的分支操作
在Worktree中可以执行大部分分支操作:
- 提交更改
- 创建新分支
- 合并分支
- 查看日志和差异
但需要注意:
- 不能在Worktree中切换到其他分支(除非是新创建的分支)
- 不同的Worktree可以相互影响(如推送同一个分支)
实践操作
1. Worktree中的分支操作
# 1. 创建基础环境
git checkout main
git checkout -b feature-sharing
echo "Shared feature" > shared.txt
git add shared.txt
git commit -m "Add shared feature"
git checkout main
# 2. 创建Worktree
git worktree add ../sharing-wt feature-sharing
# 3. 在Worktree中创建新分支
cd ../sharing-wt
git checkout -b enhancement-sharing
echo "Enhancement" > enhancement.txt
git add enhancement.txt
git commit -m "Add enhancement"
# 4. 在主仓库中查看分支状态
cd ../git-worktree-demo
git branch -a
# 会看到feature-sharing和enhancement-sharing分支
# 5. 在主仓库中切换到新分支
git checkout enhancement-sharing
# 可以看到enhancement.txt文件
2. 跨Worktree协作
# 1. 创建多个Worktree用于协作模拟
git checkout main
git checkout -b collaboration-feature
git worktree add ../collab-wt1 collaboration-feature
git worktree add ../collab-wt2 collaboration-feature
# 2. 在第一个Worktree中进行修改
cd ../collab-wt1
echo "Work from collaborator 1" > collab1.txt
git add collab1.txt
git commit -m "Add work from collaborator 1"
git push origin collaboration-feature
# 3. 在第二个Worktree中获取更新
cd ../collab-wt2
git pull origin collaboration-feature
# 现在可以看到collab1.txt文件
# 4. 在第二个Worktree中添加自己的修改
echo "Work from collaborator 2" > collab2.txt
git add collab2.txt
git commit -m "Add work from collaborator 2"
git push origin collaboration-feature
# 5. 在第一个Worktree中获取更新
cd ../collab-wt1
git pull origin collaboration-feature
# 现在可以看到collab2.txt文件
3. 团队开发中的Worktree使用
场景:多个开发者协作开发一个大型功能
# 开发者1的操作
# 1. 克隆仓库并创建功能分支
git clone <repository-url> feature-development
cd feature-development
git checkout -b large-feature
# 2. 推送分支以便其他开发者使用
git push origin large-feature
# 3. 创建Worktree用于并行开发不同模块
git worktree add ../frontend-wt large-feature
git worktree add ../backend-wt large-feature
# 4. 在前端Worktree中开发
cd ../frontend-wt
# 前端开发工作...
echo "Frontend implementation" > frontend.txt
git add frontend.txt
git commit -m "Implement frontend components"
git push origin large-feature
# 开发者2的操作
# 1. 克隆仓库并获取功能分支
git clone <repository-url> feature-development
cd feature-development
git fetch origin
git checkout large-feature
# 2. 创建自己的Worktree
git worktree add ../testing-wt large-feature
# 3. 在测试Worktree中进行测试工作
cd ../testing-wt
# 测试工作...
echo "Test cases" > tests.txt
git add tests.txt
git commit -m "Add test cases"
git push origin large-feature
团队协作最佳实践
1. 分支命名规范
# 个人功能分支
feature/user-auth-<username>
bugfix/login-issue-<username>
# 团队共享分支
feature/payment-system-team
release/v2.1-staging
2. Worktree命名规范
# 基于用户名和功能命名
../<username>-feature-wt
../<username>-bugfix-wt
# 基于功能模块命名
../frontend-payment-wt
../backend-payment-wt
../testing-payment-wt
3. 同步策略
# 定期同步主分支更改
git checkout main
git pull origin main
# 将主分支更改合并到功能分支
git checkout feature-branch
git merge main
# 在Worktree中同步远程更改
cd ../feature-wt
git pull origin feature-branch
实际协作练习
练习1:模拟团队协作开发
# 1. 创建模拟的中央仓库
mkdir central-repo
cd central-repo
git init --bare
# 2. 开发者1克隆仓库并开始工作
cd ..
git clone central-repo developer1
cd developer1
git checkout -b user-profile-feature
# 3. 创建初始提交
echo "# User Profile Feature" > README.md
git add README.md
git commit -m "Initial commit for user profile feature"
git push origin user-profile-feature
# 4. 创建多个Worktree进行并行开发
git worktree add ../profile-frontend-wt user-profile-feature
git worktree add ../profile-backend-wt user-profile-feature
# 5. 在前端Worktree中工作
cd ../profile-frontend-wt
echo "Frontend UI implementation" > profile-ui.txt
git add profile-ui.txt
git commit -m "Implement profile UI"
git push origin user-profile-feature
# 6. 开发者2加入项目
cd ../../
git clone central-repo developer2
cd developer2
git fetch origin
git checkout user-profile-feature
# 7. 开发者2创建自己的Worktree
git worktree add ../profile-testing-wt user-profile-feature
# 8. 开发者2在测试Worktree中工作
cd ../profile-testing-wt
echo "Test cases for profile feature" > profile-tests.txt
git add profile-tests.txt
git commit -m "Add profile feature tests"
git push origin user-profile-feature
# 9. 开发者1获取更新
cd ../../developer1
git pull origin user-profile-feature
# 或者在Worktree中获取更新
cd ../profile-frontend-wt
git pull origin user-profile-feature
练习2:处理分支冲突
# 1. 创建冲突场景
# 在开发者1的前端Worktree中
cd ../../developer1
git worktree add ../conflict-demo-wt user-profile-feature
cd ../conflict-demo-wt
echo "Developer 1's changes" > conflict-file.txt
git add conflict-file.txt
git commit -m "Developer 1 changes"
git push origin user-profile-feature
# 2. 在开发者2的环境中制造冲突
cd ../../developer2/profile-testing-wt
echo "Developer 2's changes" > conflict-file.txt
git add conflict-file.txt
git commit -m "Developer 2 changes"
# 3. 尝试推送(会产生冲突)
# git push origin user-profile-feature # 这会失败
# 4. 拉取远程更改并解决冲突
git pull origin user-profile-feature
# 此时会提示冲突,需要手动解决
# 5. 解决冲突后提交
# 编辑conflict-file.txt解决冲突
git add conflict-file.txt
git commit -m "Resolve merge conflict"
git push origin user-profile-feature
协作注意事项
1. 沟通协调
- 在创建共享分支前与团队沟通
- 定期同步工作进度
- 及时推送已完成的工作
2. 冲突预防
# 定期同步主分支
git checkout main
git pull origin main
git checkout feature-branch
git merge main
# 在推送前先拉取
git pull origin feature-branch
git push origin feature-branch
3. 工作保护
# 在重要Worktree上使用锁定
git worktree lock ../important-feature-wt
# 定期备份重要工作
git push origin feature-branch
练习任务
任务1:团队协作模拟
- 搭建模拟的中央仓库
- 模拟两个开发者协作开发
- 使用Worktree进行并行开发
- 处理代码同步和冲突
任务2:分支管理练习
- 创建多个相关分支
- 在不同Worktree中进行分支操作
- 练习分支合并和冲突解决
- 验证跨Worktree的分支一致性
小结
本课我们学习了Git Worktree与分支管理的关系,以及在团队协作中如何有效使用Worktree。通过实践操作,我们掌握了跨Worktree协作的技巧和注意事项。下一课我们将学习Worktree的最佳实践和故障排除方法。