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 进mergeexample git checkout master git merge feature-branch

fast 进merge 特点:

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以及它们 共同祖先.

# 三方mergeexample git checkout master git merge --no-ff feature-branch

三方merge 特点:

1.3 压缩merge (Squash Merge)

压缩merge将目标branch many 个submitting压缩成一个submitting, 然 after merge to 当 before branch.

# 压缩mergeexample git checkout master git merge --squash feature-branch git submitting -m "Squash merge feature-branch"

压缩merge 特点:

2. conflict产生 原因

当Git无法自动merge两个branch 更改时, 就会产生conflict. conflict通常发生 in 以 under circumstances:

3. conflict 表现形式

当merge产生conflict时, Git会 in commands行in显示conflictinformation, 并 in conflictfilein添加特殊标记.

Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then submitting the result.

conflictfilein会package含class似以 under 标记:

<<<<<<< HEAD This is the content from the current branch. ======= This is the content from the merged branch. >>>>>>> feature-branch

其in:

4. conflict解决 步骤

解决conflict basic步骤such as under :

4.1 查看conflictstatus

using git status commands可以查看哪些file产生了conflict.

# 查看conflictstatus git status

4.2 编辑conflictfile

打开conflictfile, 手动编辑以解决conflict. 可以using文本编辑器 or 专门 conflict解决tool.

4.3 标记conflict已解决

using git add commands将解决conflict after file添加 to stage区.

# 标记conflict已解决 git add README.md

4.4 completionmerge

using git submitting commandscompletionmerge.

# completionmerge git submitting -m "Merge feature-branch and resolve conflicts"

4.5 取消merge

such as果 in merge过程in遇 to issues, 可以using git merge --abort commands取消merge, restore to merge before status.

# 取消merge git merge --abort

5. conflict解决tool

除了手动编辑conflictfile out , 还可以using专门 conflict解决tool来helping解决conflict.

5.1 Git in 置 conflict解决tool

Git in 置了一些conflict解决tool, 可以through git mergetool commandsusing.

# 启动Git in 置 conflict解决tool git mergetool

5.2 常用 conflict解决tool

6. conflict解决 best practices

以 under is 一些conflict解决 best practices:

7. merge策略

Gitproviding了 many 种merge策略, 可以through -s parameter指定.

7.1 递归策略 (Recursive)

递归策略 is Git默认 merge策略, 适用于merge两个branch.

# using递归策略merge git merge -s recursive feature-branch

7.2 章鱼策略 (Octopus)

章鱼策略适用于同时merge many 个branch, 最 many supportmerge255个branch.

# using章鱼策略merge many 个branch git merge -s octopus branch1 branch2 branch3

7.3 ours 策略

ours策略 in merge时ignore来自mergebranch 更改, 只保留当 before branch 更改.

# usingours策略merge git merge -s ours feature-branch

7.4 subtree 策略

subtree策略适用于merge子project, 常用于子modulemerge.

# usingsubtree策略merge git merge -s subtree feature-branch

实践case: 解决Gitconflict

  1. 确保当 before in master branch: git checkout master
  2. creation并切换 to new functionsbranch: git checkout -b feature-conflict
  3. in feature-conflict branch on modify README.md file 第一行
  4. submitting更改: git add README.md && git submitting -m "Modify README in feature branch"
  5. 切换回 master branch: git checkout master
  6. in master branch on modify README.md file 同一行, using不同 in 容
  7. submitting更改: git add README.md && git submitting -m "Modify README in master branch"
  8. 尝试merge feature-conflict branch: git merge feature-conflict
  9. 观察conflict产生 information
  10. using文本编辑器打开 README.md file, 手动解决conflict
  11. 标记conflict已解决: git add README.md
  12. completionmerge: git submitting -m "Merge feature-conflict and resolve conflicts"
  13. 查看merge after submittinghistory: git log --oneline --graph

互动练习

请completion以 under 练习, 巩固Gitmerge and conflict解决:

  1. creation两个 new branch: feature1 and feature2
  2. in feature1 branch on modify README.md file 同一部分
  3. in feature2 branch on modify README.md file 同一部分, using不同 in 容
  4. feature1 branchmerge to master branch
  5. 尝试将 feature2 branchmerge to master branch, 观察conflict
  6. using文本编辑器解决conflict
  7. completionmerge并查看submittinghistory
  8. 尝试using git mergetool 解决另一个conflict
  9. 练习using git merge --abort 取消merge
  10. 尝试using压缩merge将 many 个submitting压缩成一个submitting

completion练习 after , 你应该able to熟练MasterGitmerge and conflict解决 basicoperation!