Git 分支管理与合并策略
一、分支管理策略
合并分支时,Git 默认倾向于使用 Fast Forward 模式。大家应该都记得,一旦启用这种模式,删除分支后查看历史,就看不出最新提交是 merge 进来的还是正常提交的,分支信息会丢失。
但在解决冲突的场景下,Git 会生成一个新的提交节点,最终状态不再是 Fast Forward。这样做的好处很明显:从分支历史里能清晰看出合并痕迹。
例如,即使删除了 dev1 分支,master 依然保留了由其他分支合并的记录:
lighthouse@VM-8-10-ubuntu:gitcode$ git log --graph --pretty=oneline --abbrev-commit
* cb7ce27 (HEAD -> master) merge book |
* da3c3b1 modify book
* | fc26892 modify book
|/
Git 支持强制禁用 Fast Forward 模式,这样每次 merge 都会生成一个新的 commit,保留完整的分支脉络。下面实战一下 --no-ff 方式的 git merge。
首先创建新分支 dev2 并切换:
lighthouse@VM-8-10-ubuntu:gitcode$ git checkout -b dev2
Switched to a new branch 'dev2'
修改 book 文件并提交:
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314 Hello World hello version1 hello version2 hello version3 write bbb for new branch a,b,c,d
lighthouse@VM-8-10-ubuntu:gitcode$ git add .
lighthouse@VM-8-10-ubuntu:gitcode$ git commit -m "modify Readme"
[dev2 2bd7b8b] modify Readme 1 file changed, 1 insertion(+)
切回 master 进行合并,注意加上 -m 参数描述合并意图:
lighthouse@VM-8-10-ubuntu:gitcode$ git checkout master
Switched to branch 'master'
lighthouse@VM-8-10-ubuntu:gitcode$ git merge --no-ff -m "merge with no-ff" dev2
Merge made by the 'ort' strategy.
book | 1 + 1 file changed, 1 insertion(+)
lighthouse@VM-8-10-ubuntu:gitcode$ cat book
Hello Island1314 Hello World hello version1 hello version2 hello version3 write bbb for new branch a,b,c,d
提示:
--no-ff参数表示禁用快进模式。禁用后合并会创建新的 commit,所以务必加上-m参数说明。











