我使用 git
和 gerrit
一段时间了,但有一种行为让我很恼火。
如果我对冲突进行cherry-pick
或 merge
,则change-ID
初始化by commit-msg hook
不在最后一行。这是一个例子。
Commit message
Change-ID: AAAAAAAAAA
Conflits:
File1.cpp
如果我保留这样的消息,则 push
会被最后一段下的 gerrit
禁止。
我知道可以使用--amend
或交互式 rebase
来编辑提交消息,但我不想这样做。我希望 git 单独处理它。
我的问题很简单。有没有办法通过在 Change-ID
之前插入冲突行?
Git 版本 1.8.1
最佳答案
解决此问题的两种方法:
(I) 准备提交消息钩子(Hook)
使用此钩子(Hook)的步骤:
复制现有示例:
cp .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg
确保 Hook 具有正确的权限:
chmod 755 .git/hooks/prepare-commit-msg
如果您在 .git/hooks
中没有它,则可以使用它。 :
#!/bin/sh
#
# An example hook script to prepare the commit log message.
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source. The hook's purpose is to edit the commit
# message file. If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, rename this file to "prepare-commit-msg".
# This hook includes three examples. The first comments out the
# "Conflicts:" part of a merge commit.
#
# The second includes the output of "git diff --name-status -r"
# into the message, just before the "git status" output. It is
# commented because it doesn't cope with --amend or with squashed
# commits.
#
# The third example adds a Signed-off-by line to the message, that can
# still be edited. This is rarely a good idea.
case "$2,$3" in
merge,)
/usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
# ,|template,)
# /usr/bin/perl -i.bak -pe '
# print "\n" . `git diff --cached --name-status -r`
# if /^#/ && $first++ == 0' "$1" ;;
*) ;;
esac
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
下面的内容应该注释掉冲突部分,而您不必费力升级 git 本身。
case "$2,$3" in
merge,)
/usr/bin/perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1" ;;
(二)升级git到2.1.3+
参见commit 96f78d3
(由 Junio C Hamano -- gitster
-- 于 2014 年 10 月 28 日 merge )
查看提交历史记录:
49c3e92 (tag: refs/tags/v2.1.3) Git 2.1.3
ebc2e5a Merge branch 'jk/pack-objects-no-bitmap-when-splitting' into maint
9db1838 Merge branch 'da/mergetool-meld' into maint
af1b4e3 Merge branch 'rm/gitweb-start-form' into maint
27c31d2 Merge branch 'bc/asciidoc-pretty-formats-fix' into maint
a8f01f8 Merge branch 'rs/daemon-fixes' into maint
5b509df Update draft release notes to 2.2
9ce57f1 Merge branch 'da/difftool'
e82935d Merge branch 'rb/pack-window-memory-config-doc'
7654ca6 Merge branch 'mg/lib-gpg-ro-safety'
ce71c1f Merge branch 'dm/port2zos'
c1777a2 Merge branch 'oc/mergetools-beyondcompare'
d70e331 Merge branch 'jk/prune-mtime'
853878d Merge branch 'bc/asciidoctor'
96ef1bd api-run-command: add missing list item marker
8828f29 use child_process_init() to initialize struct child_process variables
5d222c0 receive-pack: avoid minor leak in case start_async() fails
261f315 merge & sequencer: turn "Conflicts:" hint into a comment
从 2.1.3 版开始,该更改应该是 Git 的一部分。因此,如果您升级到 git 版本 2.1.3+,“冲突”应该会自动注释掉,使“Change-Id”成为提交消息中的最后一行,从而解决您的问题。
关于 merge 冲突后git更改change-id的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39977033/