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. js 非IE火狐插件检测

    js检测代码Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  2. linq学习三个实例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. CloudStack API访问权限控制

    在我写开始之前,请先看下CS中国社区的一篇文章http://www.cloudstack-china.org/2012/12/1465.html,在第1点里讲了关于权限级别,command属性文件位置 ...

  4. 2015北大夏令营day1 B:An Idea of Mr. A

    题意:给定一个范围l,r计算i,j(i<j)属于这个范围内的gcd(2^(2^i)+1,2^(2^j)+1)的总和. 思路:费马数的应用,让我惊呆的是当年居然有123个人会做,我tm毛都不会.. ...

  5. Non-unique Elements

    Non-unique Elements You are given a non-empty list of integers (X). For this task, you should return ...

  6. Unix/Linux环境C编程入门教程(39) shell命令之系统管理

    df命令 用于检测文件系统的磁盘空间占用和空余情况,可以显示所有文件系统对节点和磁盘块的使用情况.命令的使用格式如下: df  [选项] 常用参数及含义如下表所示. df -a:显示所有文件系统的磁盘 ...

  7. jquery中table里面的tr里的input添加一行,并且第一列autoincrement

    实现添加一行并且第一列由A0开始autoincrement,代码如下(在文件的同一个文件夹下添加一个jquery.js文件): <!DOCTYPE html PUBLIC "-//W3 ...

  8. linux中patch命令 -p 选项

    patch命令和diff命令是linux打补丁的成对命令,diff 负责生产xxxxx.patch文件,patch命令负责将补丁打到要修改的源码上.但是patch命令的参数-p很容易使人迷惑,因为对- ...

  9. 简单dp hdu-4105-Electric wave

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4105 题目意思: 给一个字符串,求最大的分隔空格数,记所有被分隔的数为a1,a2,a3,..... ...

  10. windows可以登录qq,但无法打开浏览器页面

    1.检查dns是否配置. 2.检查hosts是否有异常. 3.电脑上装有抓包软件,为IE设置代理服务器了. 4.360网络修复.