git merge 分支 合并 策略
2018-08-25 18:21
1446 查看
场景需求
主分支(master)合并特性分支(dev)的更改到主分支,同时忽略其中某些文件。比如说忽略配置文件,这个比较常见。为了解决这个问题,我们可以利用 git 提供的合并策略来实现。
创建并开启自定义驱动
master# git config --global merge.ours.driver true
配置驱动策略
master# echo 'index.php merge=ours' >> .gitattributes master# git add .gitattributes master# git commit -m 'chore: Preserve index.php during merges'
查看策略配置
master# ls -al master# cat .gitattributes index.php merge=ours
测试合并策略
master# git checkout dev dev# git rebase master 这里有冲突解决冲突,然后合并index.php, 再修改index.php中的内容和master中的index.php内容不同,待会儿查看结果。 dev# git checkout master master# git merge -s ours dev
查看结果
master主分支和dev分支的index.php内容不同,dev分支index.php并没有合并到master分支中,说明我们的合并策略生效。
注意问题
如果是要忽略目录,比如是src/,需要写成src/**,这点和.gitignore中的写法略有区别如果采用rebase方式合并,一定要注意启用 git config --global pull.rebase true
目的分支(master)内容可以合并到参考分支,参考分支(dev)中被加入到合并策略的文件不会合并到目的分支,
也就是说 master 中的可以合并到dev,dev中的不能合并到master,这也是我们的初衷。
参考文档
https://www.jianshu.com/p/cec1539c0a33https://segmentfault.com/a/1190000004965378
https://git-scm.com/docs/git-merge#_description
https://git-scm.com/docs/gitattributes
https://blog.yowko.com/2017/05/git-attributes-merge-strategy.html?m=1
相关文章推荐
- 分布式版本控制系统Git------分支管理与合并(merge与rebase)
- git merge 合并分支
- Git怎样撤销一次分支的合并Merge
- git如何merge(合并)代码到另外的分支
- Git怎样撤销一次分支的合并Merge
- git合并分支或者push时,报错:“Please enter a commit message to explain why this merge is necessary,especi”的解决办法
- git rebase & merge 将其他分支的修改合并到当前分支
- Git怎样撤销一次分支的合并Merge
- git 操作分支合并 merge和rebase的图文详解
- GIT 分支的合并 与 分支删除 (merge与delete)
- git 合并分支出现 Please enter a commit message to explain why this merge is necessary.
- Git教程----5.0分支管理 5.1创建与合并分支 5.2解决冲突 5.3分支管理策略 5.4Bug分支 5.5Feature分支 5.6多人协作
- git 合并分支出现 Please enter a commit message to explain why this merge is necessary.
- Git怎样撤销一次分支的合并Merge
- git 下如何把另外一个分支的某个文件改动merge 合并到这个分支,而不是把整个分支merge过来
- Git怎样撤销一次分支的合并Merge
- git命令之git merge 合并分支
- Git怎样撤销一次分支的合并Merge
- git 合并分支出现 Please enter a commit message to explain why this merge is necessary.
- Git merge和git branch 分支与合并