Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.gitattributes & individual merge strategy for a file

Tags:

git

I have a master and a test branch of my (web) application. These projects are almost the same, except for one file which sets up the application, say "setup".

Whenever I merge one branch into the other, I would like that branch to keep its version of setup. That is, git should not attempt to merge the changes to that file.

I followed the guidance from the Pro Git book and created a .gitattributes file, with the line "setup merge=ours". However, this does not work - neither with fast forward merges not if I introduce conflicts.

(To be precise:

$: mkdir gitest
$: cd gittest
$: git init
$: echo "setup merge=ours" >> .gitattributes 
$: echo "master" >> setup
$: git add setup .gitattributes
$: git commit -a -m ...
$: git branch test
$: git checkout test
$: echo "test" >> setup
$: git commit -a -m ...
$: git checkout master
$: git merge test

Expected result: setup contains the word "master", instead git performs a ff merge and setup is "test'.)

like image 222
Ian Avatar asked Mar 28 '11 21:03

Ian


People also ask

What is a .gitattributes file?

A gitattributes file is a simple text file that gives attributes to pathnames. Each line in gitattributes file is of form: pattern attr1 attr2 ... That is, a pattern followed by an attributes list, separated by whitespaces. Leading and trailing whitespaces are ignored.

Where is .gitattributes located?

These path-specific settings are called Git attributes and are set either in a . gitattributes file in one of your directories (normally the root of your project) or in the . git/info/attributes file if you don't want the attributes file committed with your project.

How do I open a .gitattributes file?

If you cannot open your GITATTRIBUTES file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a GITATTRIBUTES file directly in the browser: Just drag the file onto this browser window and drop it.

Can Git track binary files?

Git can usually detect binary files automatically. No, Git will attempt to store delta-based changesets if it's less expensive to (not always the case). Submodules are used if you want to reference other Git repositories within your project.


1 Answers

I had the same error and it can be solved just defining an "ours" merge driver in .git/config:

[merge "ours"]
    name = "Keep ours merge"
    driver = true

Since true always return 0 the temporary file holding the current state will not be changed and will stay as the final version.

You can read more about merge driver in here: http://www.kernel.org/pub/software/scm/git/docs/gitattributes.html#_defining_a_custom_merge_driver

Addendum:

This works any time the driver is actually called and this seems to occur only when there are commits changing the same files (git the merge attribute). If there are changes in a single branch the driver is not going to be called.

like image 61
Jorge E. Cardona Avatar answered Sep 19 '22 14:09

Jorge E. Cardona