git workflow: Can I prevent a certain file from being merged to another branch but still keep it under version control?

[亡魂溺海] 提交于 2019-11-30 01:32:53
Ankur

I found an answer on another question on Stack Overflow, credit goes to fcurella:

Let's say you want to exclude the file config.php

On branch A:

  1. Create a file named .gitattributes in the same dir, with this line: config.php merge=ours. This tells git what strategy to use when merging the file. In this case it always keep your version, ie. the version on the branch you are merging into.

  2. Add the .gitattributes file and commit

On branch B: repeat steps 1-2

Try merging now. Your file should be left untouched.

This seems like a more scalable solution.

Ankur, try this:

Assuming you're checked out on the live branch, run:

git merge --no-commit --no-ff stage

This will merge the two branches, but instead of making a new commit, will fail and leave these changes in your index file and working directory. Now since you want to keep your .htaccess file intact on the live branch, you can checkout this file from live:

git checkout live .htaccess

Now your working tree has all the changes from stage with the exception of .htaccess, and so you can finalize the merge commit:

git commit -m "Pulled changes from stage" -a
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!