Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git default merge commit message not including conflicts

Tags:

git

merge

commit

After doing a merge from the origin/base branch into my feature branch, I had to resolve one conflict on the file Parameter.java. I launched my Git merge tool and I resolved it. Once it was resolved, I performed a git commit and this opened Vim with the default merge commit message.

Thing is, this default commit message contains the list of conflicts, but starting with #, therefore they will be ignored in the commit message.

Merge remote-tracking branch 'origin/base' into feature

# Conflicts:
#       Parameter.java
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch feature
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
#       modified:   Parameters.java
#       modified:   SpecialParameters.java
#       modified:   Traveller.java

Is there some config to add to put these conflicts lines automatically in the commit message? Therefore removing the # on the conflicting files in the Conflicts part?

like image 548
jeerbl Avatar asked Jan 20 '16 12:01

jeerbl


2 Answers

You can use the prepare-commit-msg hook to do so.

Copy .git/hooks/prepare-commit-msg.sample to .git/hooks/prepare-commit-msg

The example inside it actually adds # to the Conflicts part:

case "$2,$3" in
  merge,)
    /usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;

This hook makes sense because previous versions were doing this by default (like in Linux git version 1.7.10.4).

Now what you want to do is exactly the opposite: removing # on the Conflicts part. Indeed, the git version 2.6.2.windows.1 comments out by default the Conflicts part so you can just update the command in prepare-commit-msg with:

/usr/bin/perl -i.bak -ne 's/^#// if /^# Conflicts/ .. /^#\R/; print' "$1" ;;
like image 163
Igal S. Avatar answered Sep 17 '22 19:09

Igal S.


I found a way to do this without any hooks or scripts: Use ---cleanup scissors:

% git commit --cleanup scissors

This results in a default commit message of:

Merge branch 'branch'

# Conflicts:
#       baz.txt
#       foo.txt
# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.


# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# On branch master
# All conflicts fixed but you are still merging.
#
# Changes to be committed:
#       modified:   bar.txt
#       modified:   baz.txt
#       modified:   foo.txt
#

And if you just accept this, you get the following commit message:

% git log -1
commit 64425eab687f9d4fc531da69495dcb401372104b (HEAD -> master)
Merge: efd152d 474a8f4
Author: Dave Dribin <[email protected]>
Date:   Fri Oct 19 23:47:19 2018 -0500

    Merge branch 'branch'

    # Conflicts:
    #       baz.txt
    #       foo.txt

This doesn't remove the # prefix, but it does include the list of conflicted files. Why does this work? It will cut everything after the special "scissors" line, rather than a comment string prefix. Here's the documentation for --cleanup scissors from the git-commit(1) man page

       --cleanup=<mode>
       This option determines how the supplied commit message should be
       cleaned up before committing. The <mode> can be strip, whitespace,
       verbatim, scissors or default.

       strip
           Strip leading and trailing empty lines, trailing whitespace,
           commentary and collapse consecutive empty lines.

       whitespace
           Same as strip except #commentary is not removed.

       verbatim
           Do not change the message at all.

       scissors
           Same as whitespace except that everything from (and including)
           the line found below is truncated, if the message is to be
           edited. "#" can be customized with core.commentChar.

               # ------------------------ >8 ------------------------

       default
           Same as strip if the message is to be edited. Otherwise
           whitespace.

       The default can be changed by the commit.cleanup configuration
       variable (see git-config(1)).
like image 44
Dave Dribin Avatar answered Sep 19 '22 19:09

Dave Dribin