Git分支合并

       当我们用Git协同工作时,通常是有多条分支的,例如,master,dev,feature1等。master分支是主分支,是我们最重要的分支,dev分支是开发分支,在dev分支上完成开发工作的,如果dev开发完毕了,就得用master分支去合并dev分支。这个时候就需要merge操作了。

      下面就让我们了解一个完成的开发到合并的流程。

      1.建立新的分支dev,创建新文件abab.txt,提交。

$ git checkout -b dev
Switched to a new branch 'dev'
$ touch abab.txt
$ echo "abab" >abab.txt
$ git add .
warning: LF will be replaced by CRLF in abab.txt.
The file will have its original line endings in your working directory.

$ git commit -m "add abab.txt"
[dev d5ada67] add abab.txt
warning: LF will be replaced by CRLF in abab.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 abab.txt

   2.切换到master分支,然后合并feature1分支。

$ git checkout master
Branch master set up to track remote branch master from origin.
Switched to a new branch 'master'

$ git merge dev
Updating 064968e..d5ada67
Fast-forward
 2.txt    | 2 +-
 abab.txt | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 abab.txt

  可以看到合并成功了,并且使用的是Fast-forward模式的合并。

     3.查看合并分支情况

$ git log --graph --pretty=oneline --abbrev-commit
* d5ada67 add abab.txt
* e416f28 add something new in 2.txt
* 064968e add dev.txt in dev

  可以看到在dev中commit -m的信息直接到master中了,从这个分支合并的图中根本看不出是从别的分支中合并过来的,就在是在master分支中提交的一样。这就是Fast-forward合并的情况。

     4.删除分支

$ git branch -d dev

      以下用图来表明上述整个过程。

     初始状态:在master分支下,HEAD指向最新的版本

      建立dev分支,HEAD指向dev的最新的版本

      在dev分支中提交abab.txt

      在master分支中合并dev分支

       删除dev分支

 

      可以看到master分支在合并的时候,master是直接指到dev分支上去的。并没有在自己的分支上去做一份commit。能否不采用fast-forwards合并呢?

      下面来看不采用fast-forwards合并的情况。

       省略建立分支dev2,创建文件nba.txt,提交的过程,直接到master中合并dev2的时候。

$ git merge --no-ff -m "merge with no-ff" dev2
Merge made by the 'recursive' strategy.
 nba.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 nba.txt

  使用--no-ff可以指定不使用fast-forwards方式合并。

       查看合并情况。

$ git log --graph --pretty=oneline --abbrev-commit
*   21494a8 merge with no-ff
|
| * db0d081 add nba
|/
* d5ada67 add abab.txt
* e416f28 add something new in 2.txt
* 064968e add dev.txt in dev

  与之前使用fast-forwards合并的情况一对比,就发现这两种情况的不同了,no-ff的合并是能看到明显的与其他分支合并的情况的。其他的分支进行的修改就是“add nba”。并且使用no-ff合并相当于在master合并了dev2后,又自己commit了一次。这种方法比ff好的 地方在于保证了master主线的完整性,不会在你删掉分支后丢失分支的信息,但是缺点就是不如ff那么快。

       以下是合并的图解

总结一下:1.当你的分支是有新的功能点的时候,系统合并默认是采用ff模式的。

                  2.可以使用--no-ff来强制不采用ff模式合并。这样可以保证master主线的完整性。

参考:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013758410364457b9e3d821f4244beb0fd69c61a185ae0000  https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840038939c291467cc7c747b1810aab2fb8863508000

原文地址:https://www.cnblogs.com/bocurry/p/7764565.html