git的合并策略总共有3种,一种是resovle,一种是recursive,一种是octopus。其中resolve和recursive适用于合并2个branch,octopus适用于合并3个或者3个以上的branch。对于这3中策略,都需要涉及到merge-base commit,ours commit和theirs commit,即3-way mege。

3-way merge

如下图所示,假设要将branch B合并到branch A,那么branch A的tip commit就是ours commit,branch B的tip commit就是theirs commit,而两个branch的公共commit(即图中有阴影的commit)就是merge-base。合并的时候就是将theirs commit相对于merge-base commit的改变应用到ours commit上,并产生一个新的commit。

但是如果在应用theirs commit相对于merge-base commit的改变的时,在同一区域,ours commit相对于merge-base commit也做了改变,就会产生冲突。举个例子,merge-base commit, ours commit,theirs commit里面都有同一个文件a,如下图所示:

theirs commit相对于merge-base commit的改变是将1,3之间的2修改为5,而ours commit相对于merge-base commit在1,3之间也做了修改,即插入了8,所以合并的时便会出现冲突:

1

++<<<<<<<<<ours

+2

+8

++=========

+ 5

++>>>>>>>>theirs

3

+4

在出现冲突的同时,git会在index中记录merge-base commit,ours commit,theris commit的相关文件,并以1,2,3标识,可以通过git ls-files -u命令查看。

merge-base

merge-base是branch之间的best common ancestor。common ancestor A比另一个common ancestor B better的条件是:B是A的ancestor。因此,一个best common ancestor没有任何better common ancestor。举个例子,如下图所示:

branch A和branch B之间有两个best common ancestor,就是图中加阴影的commit 1和commit 2。又如下图:

此时branch A和branch B的best common ancestor是加阴影的commit 3,而不是commit 1和comit 2,因为commit 3是commit 1和commit 2的ancestor。

利用git merge-base --all可以找出所有的best common ancestor。

recursive策略

merge的recursive 策略就是当两个branch之间有多个best common ancestor的时,git先临时合并这些best common ancestor,然后将这个临时产生的commit作为merge-base来合并branch。如果产生了冲突,git仍然会在index中作记录,也可以通过git ls-files -u命令来查看。

git merge的recursive策略和merge-base的更多相关文章

  1. git两种合并方法 比较merge和rebase

    18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...

  2. git push时报错refusing to merge unrelated histories

    1. 删除本地项目中的.git目录,然后向远程仓库提交代码的时候,重新配置后再次提交.会有冲突. 解决方式: git remote add origin [//your giturl] git pul ...

  3. Git中pull对比fetch和merge

    本文参考于:http://www.zhanglian2010.cn/2014/07/git-pull-vs-fetch-and-merge/ 使用git fetch和git pull都可以更新远程仓库 ...

  4. git无法pull仓库refusing to merge unrelated histories

    本文讲的是把git在最新2.9.2,合并pull两个不同的项目,出现的问题如何去解决fatal: refusing to merge unrelated histories 我在Github新建一个仓 ...

  5. git无法pull仓库refusing to merge unrelated histories (拒绝合并不相关仓库)

    原文地址 https://blog.csdn.net/lindexi_gd/article/details/52554159 本文讲的是把git在最新2.9.2,合并pull两个不同的项目,出现的问题 ...

  6. git cherry-pick 报错is a merge but no -m option was given

    gerrit上提示代码冲突的时候,我们首先会想到rebase下,不行的话就只能解决冲突了,最简单的做法是我的另一篇博客https://www.cnblogs.com/zndxall/p/9140813 ...

  7. git快速入门 push/clone/reset/merge/切换分支全都有

    本文介绍git快速入门,从安装/创建init / 发布push/版本回退reset / branch分支切换/合并分支merge 这些基本的操作都有涉及,方便新人快速入手,有需要的朋友mark一下.首 ...

  8. git在使用中出现 refusing to merge unrelated histories如何解决?

    一.GIT的使用 # 设置用户名 git config --global user.name "zhaijihai" # 设置用户邮箱 git config --global us ...

  9. git 的 pull、fetch、merge

    1.pull = fetch + merge In the simplest terms, git pull does a git fetch followed by a git merge. You ...

随机推荐

  1. mysql 8小时空闲后连接失效的解决

    查了一下发现应用程序和mysql数据库建立连接,如果超过8小时应用程序不去访问数据库,数据库就断掉连接 .这时再次访问就会抛出异常. 关于mysql自动断开的问题研究结果如下, 1.c3p0.Hika ...

  2. show_space/get_alert_log/get_trace_file

    1.get_alert_log 获取alert文件的路径和名称 set serveroutput on        --设置输出,让sqlplus在屏幕上可以输出.(要加入到login.sql中!) ...

  3. javascript第一课练习

    <!doctype html> <html lang="en">  <head>   <meta charset="UTF-8& ...

  4. NAS4Free 安装配置(一)开箱图

    拆箱记录 东西不错,做工很好 包装箱 背面 正面(未装前面板) 底部 前面板打开后 打开上盖 开机正面图

  5. 64位Ubuntu14.04搭建ADT开发环境

    本来放假是要出去玩的,结果出游计划拖到了周一,今天先来无事看看ubuntu最近发展得怎么样了,顺便把ADT也搭建好,方便以后研究android框架.想想在windows下,ADT直接down下来解压就 ...

  6. Android-2

    传递简单数据 //发送方i.putExtra("data",t.getText().toString()); //接收方Intent i = getIntent();m = (Ed ...

  7. 自定义 cell 自适应高度

    #import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation Commo ...

  8. 163k地方门户网站系统js小Bug

    163k地方门户网站系统后台在Firefox下无法通过选择区域来获取相对应的地段,IE浏览器下无些问题. Firebug提示错误为: ReferenceError: requestdataquyu i ...

  9. c++ 09

    一.数据结构 程序设计=数据结构+算法 1.逻辑结构 1)集合:元素之间没有联系. 2)线性结构:元素之间存在前后顺序. 3)树形结构:元素之间存在一对多的父子关系. 4)图状结构:元素之间存在多对多 ...

  10. Copy Constructor in Java

    Reference: TutorialPoints, GeekforGeeks The copy constructor is a constructor which creates an objec ...