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



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部分中冲突文件的#

我找到了一种无需任何钩子或脚本即可执行此操作的方法: 使用 ---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

这不会删除#前缀,但会包含冲突文件的列表。为什么会这样?它将剪切特殊"剪刀"行之后的所有内容,而不是注释字符串前缀。以下是git-commit(1)手册页中--cleanup scissors的文档

       --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)).

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

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

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

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

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

现在您要做的恰恰相反:删除冲突部分的#。事实上,默认情况下,git version 2.6.2.windows.1注释掉冲突部分,因此您只需prepare-commit-msg更新命令:

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

我使用它按模式使用 sed 的范围(并且基于 jthill 的答案)

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

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

prepare-commit-msg hook:

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

这导致

Conflicts:
    GIT-VERSION-GEN
    RelNotes

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

相关内容

  • 没有找到相关文章

最新更新