通常,合并分支时,如果没有分歧解决,就会直接移动文件指针,这就是Fast forward
模式。
举例来说,开发一直在master分支进行,但忽然有一个新的想法,于是新建了一个dev的分支,并在其上进行一系列提交,完成时,回到master分支,此时,master分支在创建dev分支之后并未产生任何新的commit。此时的合并就叫fast forward
。
但这种模式下,删除分支后,会丢掉分支信息。
如果要强制禁用Fast forward
模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。
强制禁用Fast forward
模式的具体方法
下面我们看下如何强制禁用Fast forward
模式(--no-ff
方式)的git merge
:
首先修改readme.md
文件,并提交一个新的commit:
1 2 3 4
| $ git add . $ git commit -am "update readme" [dev 237907d] update readme 1 file changed, 3 insertions(+), 1 deletion(-)
|
现在,我们切换回master:
1 2 3
| $ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'.
|
合并dev分支,请注意--no-ff
参数,表示禁用Fast forward
:
1 2 3 4 5 6 7
| $ git merge --no-ff -m "merge with no-ff" dev Merge made by the 'recursive' strategy. README.md | 4 +- ...5\217\344\275\234\346\226\207\346\241\243.docx" | Bin 278553 -> 279350 bytes ...234\200\345\212\240\345\210\206\357\274\237.md" | 62 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 "\346\225\260\346\215\256\345\210\206\346\236\220\351\235\242\350\257\225-\344\270\273\350\247\202\351\242\230/\345\275\223\351\235\242\350\257\225\345\256\230\350\257\264\357\274\214\342\200\234\344\275\240\346\234\211\344\273\200\344\271\210\350\246\201\351\227\256\346\210\221\357\274\237\342\200\235\346\200\216\346\240\267\345\233\236\347\255\224\346\234\200\345\212\240\345\210\206\357\274\237.md"
|
因为本次合并要创建一个新的commit,所以加上-m参数,把commit描述写进去。
合并后,我们用git log
看看分支历史:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ git log --graph --pretty=oneline --abbrev-commit * 1d78e1a merge with no-ff |\ | * 237907d update readme | * 75a4fbc update 主观题 | * 9669636 update 协作文档 |/ * b1aa3ed update 协作文档 * 5c7018e update readme * 85c15e2 update readme * a803ecf update readme * bb3adfb update 协作文档 * acf2104 Merge pull request |\ | * 4330188 Create LICENSE |/ * f05e852 Initial commit
|
Fast Forward和no fast foward合并模式对比图
git merge
前后对比
如果前面是采用禁用Fast forward
模式合并的,我们可以这样来比较: