Skip to content

Instantly share code, notes, and snippets.

@tmaybe
Last active February 7, 2024 03:18
Show Gist options
  • Save tmaybe/4c9d94712711229cd506 to your computer and use it in GitHub Desktop.
Save tmaybe/4c9d94712711229cd506 to your computer and use it in GitHub Desktop.
ignoring merge conflicts for specific files in a git repository

How to Ignore Merge Conflicts for Specific Files in a Git Repository

Create a directory and git init it

$ mkdir merge-test
$ cd merge-test/
$ git init

Create some simple content and commit it. When we're done, the contents of one directory will generate a conflict, the contents of the other will not.

$ mkdir allow-conflict
$ mkdir ignore-conflict
$ echo a > allow-conflict/index.txt
$ echo b > ignore-conflict/index.txt
$ git add -A
$ git commit -m 'initial commit'

Check out a new branch and modify the content of both files. This branch will represent the 'local' branch.

$ git checkout -b local-branch
$ echo a-my-conflicting-content >> allow-conflict/index.txt 
$ echo b-my-conflicting-content >> ignore-conflict/index.txt 
$ git add -A
$ git commit -m 'add content in local branch'

Check out another new branch and modify the content of both files. This branch will represent the 'remote' branch.

$ git checkout master
$ git checkout -b remote-branch
$ echo a-their-conflicting-content >> allow-conflict/index.txt 
$ echo b-their-conflicting-content >> ignore-conflict/index.txt 
$ git add -A
$ git commit -m 'add content in remote branch'

Let's verify that at this point, trying to merge the two branches will generate conflicts for both files.

$ git checkout local-branch
$ git merge remote-branch

You'll see this conflict message:

Auto-merging ignore-conflict/index.txt
CONFLICT (content): Merge conflict in ignore-conflict/index.txt
Auto-merging allow-conflict/index.txt
CONFLICT (content): Merge conflict in allow-conflict/index.txt
Automatic merge failed; fix conflicts and then commit the result.

Now let's tell git to ignore the conflict in ignore-conflict/index.txt. First, reset so we can try the merge again.

$ git reset --hard

Create a .gitattributes file in ignore-conflict that says only index.txt merge=ours

$ echo index.txt merge=ours > ignore-conflict/.gitattributes

Define an "ours" merge strategy in the git config.

$ git config merge.ours.driver true

(if you cat .git/config at this point, you'll see at the bottom:)

[merge "ours"]
    driver = true

Try to merge the branches again,

$ git merge remote-branch

and we'll see only one conflict.

Auto-merging allow-conflict/index.txt
CONFLICT (content): Merge conflict in allow-conflict/index.txt
Automatic merge failed; fix conflicts and then commit the result.

Success!

Sources:

http://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Merge-Strategies http://stackoverflow.com/a/930495/958481

@Timothy626
Copy link

Thanks for the write up. Exactly what I was looking for!

@ramyaramdasan
Copy link

Hi,

Thanks for the informative write-up. Are these merge strategies that we define using CLI also valid on GitHub.com?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment