Given you have a file with a version number which is increased in two branches, how to prevent Git from silently automerging the two lines changing lines defining the version?
* Git automatically merges here, but shouldn't
|\
| * change same line to the same new text
* | change some line here
\|
* prior history/root commit
In our case we have a database schema with migration support. Our main schema file defines the current schema version. If two people change the schema, e.g. add a database column, in different tables and both increase the schema version by +1 Git will silently merge everything but the result will be broken.
I'd suggest having a special marker to add on any line which makes Git not automerge this particular line. Is there some feature like this I don't know or how could it be achieved?
Here's a list of shell commands to create an example Git history which illustrates the problem:
$ git init test
Initialized empty Git repository in $PWD/test/.git/
$ cd test/
$ echo "version 1" > file
$ git add file
$ git commit -m "add file v1"
[master (root-commit) 4ef6950] add file v1
1 file changed, 1 insertion(+)
create mode 100644 file
$ git checkout -b a
Switched to a new branch 'a'
$ echo "version 2" > file
$ git commit -a -m "bump to v2"
[a 85dba39] bump to v2
1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout -b b master
Switched to a new branch 'b'
$ echo "version 2" > file
$ git commit -a -m "bump to v2 in b"
[b b0fcf46] bump to v2 in b
1 file changed, 1 insertion(+), 1 deletion(-)
$ git merge a
Merge made by the 'recursive' strategy.
$ git status # shouldn't be clean
On branch b
nothing to commit, working directory clean
I don't think what you ask for is possible.
An identical change is an identical change is an identical change.
There are two things that come to mind how you could workaround your issue:
Make the developers maintain a comment on the same line that bears something unique, e. g. the current time down to the second, the unix time, ... Something that is unlikely the same on both branches when the file is changed (don't use something user specific, one user could do both changes and forgot about them)
Use a pre-commit hook. In the hook check whether the commit is a merge commit and then try to determine whether there is the situation you described, where both branches changed that line and abort the merge in this case.