Git默认合并提交消息不包括冲突

时间:2016-01-20 12:07:52

标签: git merge commit

origin/base分支合并到我的feature分支后,我必须解决文件Parameter.java上的一个冲突。我启动了我的Git合并工具,我解决了它。解决之后,我执行了git commit并使用默认的合并提交消息打开了Vim。

事实是,这个默认提交消息包含冲突列表,但是从#开始,因此它们将在提交消息中被忽略。

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

是否有一些配置要添加以在提交消息中自动放置这些冲突行?因此,删除#部分中冲突文件的Conflicts

4 个答案:

答案 0 :(得分:8)

您可以使用prepare-commit-msg挂钩来执行此操作。

.git/hooks/prepare-commit-msg.sample复制到.git/hooks/prepare-commit-msg

其中的示例实际上将#添加到Conflicts部分:

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

这个钩子很有意义,因为以前的版本默认是这样做的(比如Linux git version 1.7.10.4)。

现在你要做的恰恰相反:在Conflicts部分删除#。实际上,git version 2.6.2.windows.1默认情况下注释了冲突部分,因此您可以使用以下内容更新prepare-commit-msg中的命令:

/usr/bin/perl -i.bak -ne 's/^#// if /^# Conflicts/ .. /^#\R/; print' "$1" ;;

答案 1 :(得分:3)

我找到了一种无需任何钩子或脚本的方法:使用---cleanup scissors

% git commit --cleanup scissors

这将导致默认的提交消息:

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
#

如果仅接受此操作,则会收到以下提交消息:

% git log -1
commit 64425eab687f9d4fc531da69495dcb401372104b (HEAD -> master)
Merge: efd152d 474a8f4
Author: Dave Dribin <dave@example.com>
Date:   Fri Oct 19 23:47:19 2018 -0500

    Merge branch 'branch'

    # Conflicts:
    #       baz.txt
    #       foo.txt

这不会删除#前缀,但确实包含冲突文件的列表。为什么这样做?它将在特殊的“ scissors”行之后的 之后剪切所有内容,而不是注释字符串前缀。这是--cleanup scissors man page

git-commit(1)的文档
       --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)).

答案 2 :(得分:2)

我使用的是使用sed的{​​{3}}(并基于ranges by patterns

使用人类语言阅读:在# Conflicts:#(blank line)之间的行范围内,删除前#(space)#({{1} })。

# \?

prepare-commit-msg hook:

sed -i '/start/,/stop/ s/# \?//'

导致

# prepare-commit-msg
case "$2,$3" in
  merge,)
    # Uncomment Conflicts section in merge commit body
    sed -i '/^# Conflicts:/,/^#\?$/ s/# \?//' "$1"
    ;;
  *) ;;
esac

答案 3 :(得分:-1)

您无需自己将其添加到提交消息中。一旦你做了合并提交,如果有冲突,git会告诉你它们在哪里。只需在包含冲突的合并的提交上运行git show --name-only [commit sha],您就会将其视为消息的一部分。