Search code examples
gitgit-mergegit-merge-conflictmerge-strategy

How to merge branch in git using `their` when in conflict


I have a master branch for production and a qa branch for testing.

When I develop a new feature I create a new branch from master (e.g. new_branch), use it for develop and merge it to qa for testing.

I use the following git alias:

myc = "!f() { git commit -a -m ${1} && git checkout qa && git merge ${1} && git push origin qa && git checkout ${1}; }; f"

This strategy allows me to merge into master only features that where approved by QA team.


In my code I have a define.inc.php where I set a define('_STATIC_VER_', '20'); this counter is auto-appended to all static css, js files, e.g.:

/themes/css/global.css?sv=20

So that when ever I change the css files, I increase the SV counter, therefor forcing the browser to reload all static files.

The problem is - when I have 2 branches in development n1, n2, and both made changes to define.inc.php I get

CONFLICT (content): Merge conflict in config/defines.inc.php

and I have to manually choose their version of `define.inc.php'

How can I improve my alias command to always use their when in conflict?

NOTE: git merge --strategy-option theirs doesn't solve the problem


Solution

  • git merge -s recursive -X theirs should do the trick.

    It'll always take the "incoming" branch changes when there's a conflict but behave as a classic merge for all other cases

    The full alias cmd is:

    myc = "!f() { git commit -a -m ${1} && git checkout qa && git merge -s recursive -X theirs ${1} && git push origin qa && git checkout ${1}; }; f"