user1071847 user1071847 - 25 days ago 7
Git Question

git merge: how keep master version of file?

I'm using git and am merging a branch into master after a pull request.

My understanding is that if a file in the master wasn't changed since the last common ancestor, but the same file in the branch was, git will use the version in the branch during the merge.

For most of the differences between the two branches, that's the behavior I desire, but there are a few files in one subdirectory tree for which I want the merge to keep the master version and discard the branch version.

What's a good command-line based workflow for doing that?

Answer

The easiest option is to use git merge --no-commit --no-ff to merge everything and stop at the last step before everything is committed. Then you reset/checkout those files that you need to stay the same, then run git-commit. You will need to do so whenever you make changes that you do not want merged onto master.

Another option, somewhat more complex, is to create two branches, let us call them tests and features. Branch features contains those changes that you will merge onto master, while branch tests contains the other changes. Thus you will keep merging features onto both master and tests.

A third option is to keep merging and then making commits that revert those things you did not want changed.