Git Merge — Ignore Merge Conflicts for Specific Directory

How do you have Git ignore merge conflicts for a directory?
How do you force git to always accept files from a specific branch?

Look no further.

I have all of my project files in a git repo, including my libraries. Some libraries must compile and build binaries dependent on the OS, in my example pyCrypto.

That means I have a development branch for local development on my Mac, and a production branch that has the correct pyCrypto binaries for the production server.

However, every time I try to merge changes from my development branch, I’d get conflicts (naturally) since the binaries are different between the two packages.

I’ve solved my problem by forcing git to NOT merge the Crypto folder at all

I set up a git merge driver that does nothing at all, and set all files in the Crypto folder to use this driver.

All of this info comes from a stack question here

Step 1: create a special merge rule in your repositories .git/config file

[merge "keepmine"]
        name = dont_merge_selected_files
        driver = echo %O %A %B

Step 2: navigate to the folder you want never merged from outside branches, and create a .gitattributes file where you specify all files to use the merge driver “keepmine” defined in step 1

* merge=keepmine

commit your changes, and try merging! The conflicts in the folder with .gitattributes will not register and leave the original files intact. If you want the other way around, where your branch is always overwritten by a merge, take a look at the stack post above.

It says to set up a merge driver as %O %A %B

and in your

cp -f $3 $2.
return 0

where 0 is the exit code of “resolved”.

This saves my world.

One thought on “Git Merge — Ignore Merge Conflicts for Specific Directory

  1. I’ve now updated my environment to run off a deployment script that compiles everything needed. Luckily, only a few libraries (pyCrypto not included) have to stay in the project repo due to hacks or dev releases.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s