GoZoner
GoZoner

Reputation: 70135

Forcing 'git merge' to declare all differences as a merge conflict

In a 'git merge ' I would like any difference, even if not normally a merge conflict, to be considered a merge conflict. Then, with 'git mergetool' I can see and resolve each and every difference. I tried specifying '* -merge' in .gitattributes but that doesn't seem to have worked:

$ git checkout master
Switched to branch 'master'
$ ls
foo.c
$ git merge add-on
Updating a628824..2219552
Fast-forward
  0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar.c
$ cat .gitattributes 
* -merge
$ ls
bar.c   foo.c

For the above 'git merge add-on' I expected a merge conflict for 'bar.c' with no base version, no local version and a remote version. [edit] As suggested in one answer, a merge didn't occur above. Here is a case where I forced a merge, still no desired merge conflict:

$ git merge --no-ff add-on
Merge made by the 'recursive' strategy.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bing.c

Note that in above 'bing.c' is actually empty; but, that isn't the problem because providing a non-empty file still gets merged. [edit 2] I tried --no-commit with this result:

$ git merge --no-ff --no-commit add-on
Automatic merge went well; stopped before committing as requested
$ git status
# On branch master
# Changes to be committed:
#
#   new file:   boom.c
#
$ git mergetool
No files need merging
$ cat .gitattributes 
* -merge

What am I missing? Is there a way to confirm that '.gitattributes' is getting used/read?

Upvotes: 8

Views: 3145

Answers (3)

Eponymous
Eponymous

Reputation: 6811

I have a similar desire and have been unable to do it with the automatic tools. Here is a semi automated solution:

From master, merge add-on and manually resolve changes:

Start with:

git merge --no-ff --no-commit add-on

Then, for each changed file do:

git show master:path/to/file > from_master
git show add-on/to/file > from_add-on
kdiff3 --qall from_master from_add-on -o path/to/file
git add path/to/file

Finally,

git commit

Why this 90% solution?

This solution merges even in "obvious situations" and it doesn't force me to set .gitattributes so I can make "manual merge" behavior dependent on who I am merging from and why rather than on which files I am merging.

Upvotes: 2

Lily Ballard
Lily Ballard

Reputation: 185653

You didn't merge. Your command git merge add-on executed a "fast-forward", which means it just moved the branch head. This is because your add-on branch was descended from the tip of your master branch already, so no merge was needed. If you run git log you'll see there's no merge commit.

Basically, it looked like this before the merge:

              master
             /
o---o---o---o           add-on
             \         /
              o---o---o

and the merge just moved the master pointer to the end of the line:

                          master, add-on
                         /
o---o---o---o---o---o---o

If you want to force a merge, pass the --no-ff flag, as in git merge --no-ff add-on.


Upon further reflection, the merge attribute won't do what you want. This only applies to file-level merges, which means both sides of the merge has changes to the specific file. If only one side has changes (which is your case), no file-level merge is done and the changed file is accepted unconditionally.

Your best bet is probably to use git merge --no-ff --no-commit add-on to produce the merge but don't actually commit. You can now inspect the results and tweak them to your satisfaction before committing the merge. If you want to accept changes on a per-hunk basis you can do something like git reset to reset the index and then git add -p to do per-hunk staging.

Upvotes: 6

yamen
yamen

Reputation: 15618

You may want to try:

git merge --no-commit

Which will make GIT not commit the merge changes. Alternatively if you want it to commit when there are no conflict, but not to lose the source branch, it's no-ff:

git merge --no-ff

Both can be used if necessary.

More info here: Why does git fast-forward merges by default?

Upvotes: 3

Related Questions