Git: ignore some files during a merge (keep some files restricted to one branch)

I found a good answer here: stackoverflow Q332528

It uses ideas taken from here: Pro-Git merge strategies

Here is a copy of it:

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 mergin 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.


Edit:
From the git book regarding merge=ours, "One very useful option is to tell Git to not try to merge specific files when they have conflicts, but rather to use your side of the merge over someone else’s."

So, this answer doesn't apply as well as it might to the question. pjmorse's answer regarding using submodules is good.

Another option would be to use a sub-tree merge, which may have added benefits.


You might find git's rerere command useful. With that you can record resolutions for certain merge conflicts and reuse them later.


With your updates: Yes, submodules would be appropriate for this use if all of A fits in a subdirectory of B (or vice versa). An example of submodules using WordPress would be if you have a git repository of Wordpress; you could add a submodule for a theme which would be inside the /wp-content/themes/ directory.

The documentation for submodules might help.

If the files from the two are interleaved, it might be tougher. Most cases where submodules can be used in this way, the application in question was designed to allow for them.