Skip to content

Instantly share code, notes, and snippets.

@tsukasa-mixer
Forked from tmaybe/ignore.md
Created May 30, 2019 15:04
Show Gist options
  • Save tsukasa-mixer/30724d447607d3b43af1dc93203bae59 to your computer and use it in GitHub Desktop.
Save tsukasa-mixer/30724d447607d3b43af1dc93203bae59 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

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