2016-01-20 135 views
8

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

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

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

回答

8

您可以使用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" ;; 
+0

'#该钩子包括三个例子。第一个注释是合并提交的“冲突:”部分.'写在文件的开头,所以它实际上是相反的。无论如何,我试着重新命名它,因为你建议,我重新启动Git bash,但它仍然无法正常工作,冲突部分仍然被注释掉。 – jeerbl

+0

你说得对。我编辑了我的答案。 –

+1

对于perl,替换'sed -i.bak'/ ^#冲突:/,/ ^#$/s /..//'“$ 1”' – jthill

-1

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

+0

你能举个例子吗?我已经测试过它,它似乎没有工作。据我所知,一旦提交完成,Git不会存储任何有关冲突的信息。 –

+0

你可以在这里看到一个例子:https://help.github.com/articles/resolving-a-merge-conflict-from-the-command-line/ – mohammedkhan

+0

有趣的。也许这是一个混帐版本的问题。尝试时我没有看到它。 –

1

我使用此它使用sedranges by patterns(和基于jthill's answer

读人类语言:在# Conflicts:#(blank line)之间的线的范围内,除去前述#(space)## \?)。

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

准备提交-MSG钩:

# 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