分享

Git的Fast Forward和no fast foward合并模式对比

 rongq2007 2021-09-17

详细版本见个人博客:Git的Fast Forward和no fast foward合并模式对比


通常,合并分支时,如果没有分歧解决,就会直接移动文件指针,这就是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:

$ git add .$ git commit -am "update readme"[dev 237907d] update readme
1 file changed, 3 insertions(+), 1 deletion(-)1234

现在,我们切换回master:

$ git checkout master
Switched to branch 'master'Your branch is up-to-date with 'origin/master'.123

合并dev分支,请注意--no-ff参数,表示禁用Fast forward

$ 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"1234567

因为本次合并要创建一个新的commit,所以加上-m参数,把commit描述写进去。

合并后,我们用git log看看分支历史:

$ 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 #1 from dta0502/add-license-1|\| * 4330188 Create LICENSE|/
* f05e852 Initial commit1234567891011121314151617

二、Fast Forward和no fast foward合并模式对比图

在这里插入图片描述


详细版本见个人博客:Git的Fast Forward和no fast foward合并模式对比

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/dta0502/article/details/90214465

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多