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?
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With