情景1: Git的常见分支处理方案

前置状态

现在有一个 Git仓库, 里面有 README.txt 文件 和 description.txt 文件

README.txt 文件内容

  1. This is the first line in the README.txt

description.txt 文件为空

有如下分支

  • master
  • feature
  • develop

操作方案1—不同分支处理了同一个文件,合并时产生冲突的处理

开始时 master <==> feature <==> develop (3个分支处于同一处)

切换到 feature 分支

​ 修改 read.txt 文件为

  1. This is the first in the read.txt
  2. in the feature branch

commit 2

​ commit1 commit2

修改后 init —————> master(develop)—————> feature

切换到 develop 分支,并保证工作区 与 初始状态一样

​ 修改 read.txt 文件为

  1. This is the first in the read.txt
  2. In the develop branch

commit3

​ commit1

修改后 init ————> master————> feature[处于commit2]

​ |

​ |

​ ————————> develop[处于commit3]

最后 切换到 master分支,并保证 工作区与 版本库一致(也就是最初始状态)

此处先 合并 develop 后合并feature (也可以 先合并feature后合并 develop 效果一样)

git merge feature

git merge develop

出现冲突,处理冲突文件 read.txt

同一个文件合并时的冲突都合并到了文件中,git不辅助做修改,需要自行修改,修改文件后重新commit

  1. This is the first in the read.txt
  2. <<<<<<< HEAD
  3. In the develop branch
  4. =======
  5. in the feature branch
  6. >>>>>>> feature

方案1

修改后,假设此处2句都要可以改成

  1. This is the first in the read.txt
  2. In the develop branch
  3. in the feature branch

git commit

方案2

修改后,假设此处可以改成 master

  1. This is the first in the read.txt
  2. In the dmaster branch

git commit

操作方案2—不同分支处理了不同文件,各分支间的修改相互独立没有修改到同一个文件

正常合并 (merge) 不会报出 冲突异常(conflict)。

操作方案3—不同分支修改了同一 文件的文件名

合并时报出 冲突异常

比如 read.txt 在分支 develop 改成 read1.txt

而另外一个人 操作 分支feature 时 将read.txt 改成了 read2.txt

这时 master 合并2个分支时 会出现异常,结果导致出现 read1.txt 与read2.txt 需要合理甄别,或者讨论后决定是 留住哪一个文件,还是都留下来,在 add 以及commit