Gitmerge and conflict解决
Gitmerge is 将一个branch 更改merge to 另一个branch 过程, 而conflict is merge过程in可能出现 一种circumstances, 当两个branch for 同一file 同一部分for了不同 modify时就会产生conflict. Mastermerge and conflict解决 is Gitusingin important 技能.
1. Gitmerge class型
Gitsupport many 种merge方式, 每种方式适用于不同 场景.
1.1 fast 进merge (Fast-forward Merge)
fast 进merge is 最 simple merge方式, 当目标branch is 当 before branch 直接祖先时, Git会直接将当 before branch指针指向目标branch 最 new submitting.
fast 进merge 特点:
- 不会creation new mergesubmitting
- 保持submittinghistory 线性
- 只适用于目标branch is 当 before branch 直接祖先 circumstances
1.2 三方merge (Three-way Merge)
当目标branch and 当 before branch has 不同 submittinghistory时, Git会执行三方merge, creation一个 new mergesubmitting. 三方merge需要三个submitting: 当 before branch 最 new submitting, 目标branch 最 new submitting以及它们 共同祖先.
三方merge 特点:
- 会creation一个 new mergesubmitting
- 保留branch mergehistory
- 适用于所 has merge场景
1.3 压缩merge (Squash Merge)
压缩merge将目标branch many 个submitting压缩成一个submitting, 然 after merge to 当 before branch.
压缩merge 特点:
- 将 many 个submitting压缩成一个submitting
- 保持主branchsubmittinghistory 整洁
- 需要手动submittingmerge结果
2. conflict产生 原因
当Git无法自动merge两个branch 更改时, 就会产生conflict. conflict通常发生 in 以 under circumstances:
- 同一file 同一行被modify: 两个branch for 同一个file 同一行for了不同 modify.
- 同一file被delete and modify: 一个branchdelete了file, 另一个branchmodify了该file.
- 同一file被rename for 不同名称: 两个branch将同一个filerename for 不同 名称.
3. conflict 表现形式
当merge产生conflict时, Git会 in commands行in显示conflictinformation, 并 in conflictfilein添加特殊标记.
conflictfilein会package含class似以 under 标记:
其in:
<<<<<<< HEADto=======之间 is 当 before branch in 容=======to>>>>>>> feature-branch之间 is mergebranch in 容
4. conflict解决 步骤
解决conflict basic步骤such as under :
4.1 查看conflictstatus
using git status commands可以查看哪些file产生了conflict.
4.2 编辑conflictfile
打开conflictfile, 手动编辑以解决conflict. 可以using文本编辑器 or 专门 conflict解决tool.
4.3 标记conflict已解决
using git add commands将解决conflict after file添加 to stage区.
4.4 completionmerge
using git submitting commandscompletionmerge.
4.5 取消merge
such as果 in merge过程in遇 to issues, 可以using git merge --abort commands取消merge, restore to merge before status.
5. conflict解决tool
除了手动编辑conflictfile out , 还可以using专门 conflict解决tool来helping解决conflict.
5.1 Git in 置 conflict解决tool
Git in 置了一些conflict解决tool, 可以through git mergetool commandsusing.
5.2 常用 conflict解决tool
- Visual Studio Code: in 置了强 big conflict解决functions, supportgraph形化界面解决conflict.
- Sourcetree: 免费 Gitgraph形化客户端, providing直观 conflict解决界面.
- Beyond Compare: 专业 file比较 and mergetool, support many 种file格式.
- KDiff3: open-source file比较 and mergetool, 跨平台support.
6. conflict解决 best practices
以 under is 一些conflict解决 best practices:
- 频繁merge: 定期将主线branch 更改merge to functionsbranch, reducingconflict 可能性.
- 保持submitting atomicity: 每个submitting只package含一个逻辑 on 更改, 便于conflict解决.
- using has 意义 submittinginformation: 清晰 submittinginformation has 助于understanding更改 目 , 便于conflict解决.
- usingbranch保护: in 远程仓libraryin设置branch保护, 要求code审查 and throughtest after 才能merge.
- communication协作: 当遇 to complex conflict时, and 相关Development者communication协作解决.
7. merge策略
Gitproviding了 many 种merge策略, 可以through -s parameter指定.
7.1 递归策略 (Recursive)
递归策略 is Git默认 merge策略, 适用于merge两个branch.
7.2 章鱼策略 (Octopus)
章鱼策略适用于同时merge many 个branch, 最 many supportmerge255个branch.
7.3 ours 策略
ours策略 in merge时ignore来自mergebranch 更改, 只保留当 before branch 更改.
7.4 subtree 策略
subtree策略适用于merge子project, 常用于子modulemerge.
实践case: 解决Gitconflict
- 确保当 before in
masterbranch:git checkout master - creation并切换 to new functionsbranch:
git checkout -b feature-conflict - in
feature-conflictbranch on modifyREADME.mdfile 第一行 - submitting更改:
git add README.md && git submitting -m "Modify README in feature branch" - 切换回
masterbranch:git checkout master - in
masterbranch on modifyREADME.mdfile 同一行, using不同 in 容 - submitting更改:
git add README.md && git submitting -m "Modify README in master branch" - 尝试merge
feature-conflictbranch:git merge feature-conflict - 观察conflict产生 information
- using文本编辑器打开
README.mdfile, 手动解决conflict - 标记conflict已解决:
git add README.md - completionmerge:
git submitting -m "Merge feature-conflict and resolve conflicts" - 查看merge after submittinghistory:
git log --oneline --graph
互动练习
请completion以 under 练习, 巩固Gitmerge and conflict解决:
- creation两个 new branch:
feature1andfeature2 - in
feature1branch on modifyREADME.mdfile 同一部分 - in
feature2branch on modifyREADME.mdfile 同一部分, using不同 in 容 - 将
feature1branchmerge tomasterbranch - 尝试将
feature2branchmerge tomasterbranch, 观察conflict - using文本编辑器解决conflict
- completionmerge并查看submittinghistory
- 尝试using
git mergetool解决另一个conflict - 练习using
git merge --abort取消merge - 尝试using压缩merge将 many 个submitting压缩成一个submitting
completion练习 after , 你应该able to熟练MasterGitmerge and conflict解决 basicoperation!