如何自定义 git 的合并提交消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3148863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I customize git's merge commit message?
提问by shil88
Every time I do a merge I need for a merge commit to be generated and I would like it to have more than just the summary of all the commits.
每次我进行合并时,我都需要生成一个合并提交,我希望它不仅仅是所有提交的摘要。
My question is how can I format git-fmt-merge-msg or what determines this automated message(I can do this manually after a commit by amending it and using git-log --pretty=format:'...')
我的问题是我如何格式化 git-fmt-merge-msg 或者是什么决定了这个自动消息(我可以在提交后通过修改它并使用 git-log --pretty=format:'...' 手动执行此操作)
For example I would like to format it as such:
例如,我想将其格式化为:
Merge branch 'test'
* test:
[BZ: #123] fifth commit subject
[BZ: #123] fourth commit subject
[BZ: #123] third commit subject
[BZ: #123] second commit subject
[BZ: #123] first commit subject
__________________________________________
Merge details:
[BZ: #123] fifth commit subject
at 2010-06-30 11:29:00 +0100
- fifth commit body
[BZ: #123] fourth commit subject
at 2010-06-30 11:22:17 +0100
- fourth commit body
[BZ: #123] third commit subject
at 2010-06-30 11:21:43 +0100
- third commit body
[BZ: #123] second commit subject
at 2010-06-30 11:21:30 +0100
- second commit body
[BZ: #123] first commit subject
at 2010-06-30 11:29:57 +0100
- first commit body
采纳答案by GaetaWoo
I wanted to do something just like this. I didn't find any reasonable way to get git fmt-merge-msg
to work. I think it doesn't work the way I was hoping (passing in a completely custom text to use for the message). So instead I figured out another way using the -no-commit
and commit -F
commands. The output is of course customizable but it reflects almost exactly what you said you wanted the output to be.
我想做这样的事情。我没有找到任何合理的git fmt-merge-msg
上班方式。我认为它不像我希望的那样工作(传入一个完全自定义的文本以用于消息)。所以我想出了另一种使用-no-commit
andcommit -F
命令的方法。输出当然是可定制的,但它几乎完全反映了您所说的想要输出的内容。
Sample Commit Message Output:
示例提交消息输出:
Merge branch fix4 into master
::SUMMARY::
Branch fix4 commits:
Add fix4b-4
Add fix4b-3
Add fix4b-2
Add fix4b-1
Branch master commits:
fix4b-5 on master
* * * * * * * * * * * * * * * * * * * * * * * * *
::DETAILS::
commit < 98ffa579e14610b3566e1a3f86556a04dc95a82b
Author: -----
Date: Fri Aug 17 17:23:26 2018 -0400
fix4b-5 on master
commit > 7e386dddee16a7c2588954d25dd6793cdaa1b562
Author: -----
Date: Fri Aug 17 15:18:17 2018 -0400
Add fix4b-4
use log output as commit message
commit 2e630b1998312ec1093d73f9fe77b942407f45e8
Author: -----
Date: Fri Aug 17 15:15:28 2018 -0400
Add fix4b-3
commit > 2e630b1998312ec1093d73f9fe77b942407f45e8
Author: -----
Date: Fri Aug 17 15:15:28 2018 -0400
Add fix4b-3
commit > c9bb199be49c17ca739d019d749263314f05fc46
Author: -----
Date: Fri Aug 17 15:15:27 2018 -0400
Add fix4b-2
commit > 5b622a935c9d078c7d0ef9e195bccf1f98cce5e4
Author: -----
Date: Fri Aug 17 15:15:27 2018 -0400
Add fix4b-1
And the usage would be:
用法是:
$ git mergelogmsg branch-name
I'll copy the alias here:
我将在这里复制别名:
[alias]
mergelogmsg = "!f() { var=$(git symbolic-ref --short HEAD) && printf 'Merge branch %s into %s\n\n::SUMMARY::\nBranch %s commits:\n' $var > temp_merge_msg && git log --format=format:'%s' $var.. >> temp_merge_msg && printf '\n\nBranch %s commits:\n' $var >> temp_merge_msg && git log --format=format:'%s' ..$var >> temp_merge_msg && printf '\n\n* * * * * * * * * * * * * * * * * * * * * * * * *\n::DETAILS::\n' >> temp_merge_msg && git log --left-right $var... >> temp_merge_msg && git merge --no-ff --no-commit && git commit -eF temp_merge_msg; rm -f temp_merge_msg;}; f"
If you want to copy and paste it to customize it, use the above. The below version has line breaks which you do not want but I'll use to explain what I'm doing:
如果您想复制并粘贴它以自定义它,请使用上面的。下面的版本有你不想要的换行符,但我会用它来解释我在做什么:
[alias]
1 mergelogmsg = "!f() { var=$(git symbolic-ref --short HEAD) &&
2 printf 'Merge branch %s into %s\n\n::SUMMARY::\nBranch %s commits:\n' $var > temp_merge_msg &&
3 git log --format=format:'%s' $var.. >> temp_merge_msg &&
4 printf '\n\nBranch %s commits:\n' $var >> temp_merge_msg &&
5 git log --format=format:'%s' ..$var >> temp_merge_msg &&
6 printf '\n\n* * * * * * * * * * * * * * * * * * * * * * * * *\n::DETAILS::\n' >> temp_merge_msg &&
7 git log --left-right $var... >> temp_merge_msg &&
8 git merge --no-ff --no-commit &&
9 git commit -eF temp_merge_msg; rm -f temp_merge_msg;}; f"
Alright...
好吧...
Line 1starts the custom function as a bash shell script so git knows it's not a git command. It sets the current branch (master if you are merging a different branch into master) to a variable so we can use it later.
Line 2prints the first line using the current branch and the branch name you've given the original command (just as you would in a normal merge command). It writes this to a temp file.
Line 3gets the log of the commits in the incoming branch that are not in the current branch, and writes out only the subjects of those commits to the temp file.
Line 4prints the next line to temp.
Line 5gets the log of the commits in the current branch that are not in the incoming branch, and writes out only the subjects of those commits to the temp file.
Line 6prints a little horizontal separator between the summary and detail portions.
Line 7gets the log of all the commits in the current branch and incoming branch back to the time just after they branched off from each other, or last shared an ancestor. The left-right gives an arrow that shows which branch the commit comes from. < means current branch and > means incoming branch.
Line 8executes a merge command with the incoming branch with no fast-forward (so you get a commit) and with no commit (so you have to write one yourself... ah but you don't!)
Line 9executes the commit command with the -e
and -F
parameters to allow for editing and to tell the commit to populate the message with the text in the specified file. Once you've finished the commit message as desired, it commits the merge and deletes the temp file.
第 1行将自定义函数作为 bash shell 脚本启动,因此 git 知道它不是 git 命令。它将当前分支(如果您将不同的分支合并到 master 中,则为 master)设置为一个变量,以便我们以后可以使用它。
第 2行使用当前分支和给定原始命令的分支名称打印第一行(就像在普通合并命令中一样)。它将此写入临时文件。
第 3 行获取传入分支中不在当前分支中的提交的日志,并将这些提交的主题仅写出到临时文件中。
第 4行将下一行打印到 temp。
5号线获取当前分支中不在传入分支中的提交日志,并仅将这些提交的主题写出到临时文件中。
第 6行在摘要和详细信息部分之间打印了一个水平分隔符。
第 7行将当前分支和传入分支中所有提交的日志返回到它们彼此分支或最后共享一个祖先之后的时间。左右给出了一个箭头,显示提交来自哪个分支。< 表示当前分支,> 表示传入分支。
第 8 行与传入分支执行合并命令,没有快进(所以你得到一个提交)和没有提交(所以你必须自己写一个......啊,但你没有!)
第 9 行使用-e
和-F
参数执行提交命令以允许编辑并告诉提交使用指定文件中的文本填充消息。根据需要完成提交消息后,它会提交合并并删除临时文件。
Tada! The two ;
at the end of that long command make it so the printf functions do not write out to the console, only to the file.
多田!该;
长命令末尾的两个使 printf 函数不会写出到控制台,而只会写到文件中。
回答by laszlok
I'm aware this isn't answering the original question, but for the benefit of git noobs like myself who reach this page because it's currently the first Google result for "git change merge commit message", I'll mention that it is possible to:
我知道这不是在回答最初的问题,但是为了像我这样到达此页面的 git noobs 的利益,因为它目前是“git change merge commit message”的第一个谷歌结果,我会提到这是可能的到:
git commit --amend -m"New commit message"
to change the commit message of a merge commit without losing the link to any of the parents of the merge commit.
更改合并提交的提交消息,而不会丢失与合并提交的任何父项的链接。
回答by R0MANARMY
Looks like as of version Git 1.7.8you can do git merge --edit ...
to specify the commit message.
看起来从Git 1.7.8版本开始,您可以git merge --edit ...
指定提交消息。
And as of 1.7.10, dropping into edit mode will be the default behavior
从1.7.10 开始,进入编辑模式将是默认行为
From this release on, the "git merge" command in an interactive session will start an editor when it automatically resolves the merge for the user to explain the resulting commit, just like the "git commit" command does when it wasn't given a commit message.
从这个版本开始,交互式会话中的“git merge”命令将启动一个编辑器,当它自动为用户解析合并以解释结果提交时,就像“git commit”命令在没有给出一个提交消息。
(though I'm not seeing it on in msysgit on windows).
(虽然我没有在 Windows 上的 msysgit 中看到它)。
回答by shil88
I've found there are two ways for solving this problem
我发现有两种方法可以解决这个问题
note: don't use both at the same time, as if the commit fails to merge it will add the log again to the bottom.
注意:不要同时使用两者,因为如果提交未能合并,它将再次将日志添加到底部。
personal note: I'm using the first solution as it relies entirely on git's hooks and config properties, instead of an external script.
For a real solution one would have to extend a git command named 'fmt-merge-msg' that generates the oneline descriptions when passing the --log option (if you really need this solution you'll have to create your own patch (for git) and compile it from source).
个人说明:我正在使用第一个解决方案,因为它完全依赖于 git 的钩子和配置属性,而不是外部脚本。
对于真正的解决方案,必须扩展名为“fmt-merge-msg”的 git 命令,该命令在传递 --log 选项时生成单行描述(如果您确实需要此解决方案,则必须创建自己的补丁(对于git) 并从源代码编译它)。
1. using prepare-commit-message as VonC suggested
this solution has the problem that you need to interrupt the commit and then commit manually
1.使用prepare-commit-message作为VonC建议
这个解决方案有问题,你需要中断提交然后手动提交
setting an alias that will build the desired commit message:
设置将构建所需提交消息的别名:
[alias]
lm = log --pretty=format:'%s%n by %C(yellow)%an%Creset (%ad)%n %n%b' --date=local
creating the prepare-commit-msg hook by creating an executable prepare-commit-msg in $GIT_DIR/hooks/ (example script below)
通过在 $GIT_DIR/hooks/ 中创建一个可执行的 prepare-commit-msg 来创建 prepare-commit-msg 钩子(下面的示例脚本)
#!/bin/sh
#...
case "," in
merge,)
echo "Merge details:" >>
echo "" >>
git lm ORIG_HEAD..MERGE_HEAD >> "" ;;
*) ;;
esac
one should define an alias commit msg such as
应该定义一个别名提交消息,例如
[alias]
m = merge --no-ff --no-commit
2. using a custom command that will generate the merge automatically
(using the lm alias created in 1.)
2.使用将自动生成合并的自定义命令
(使用在 1 中创建的 lm 别名。)
#!/bin/sh
echo ""
echo "merge with commit details -- HEAD.."
git merge --no-ff --no-log -m "`git lm HEAD..`" --no-commit
and then execute a rather rigid command:
然后执行一个相当严格的命令:
./cmd-name <branch to merge>
if you still wish to have the oneline description of the commits you'll need to add new commands or whatever to the -m argument (if you use --log then it will be generated on the bottom)
如果您仍然希望对提交进行单行描述,则需要向 -m 参数添加新命令或其他内容(如果您使用 --log 则它将在底部生成)
回答by przbadu
Once you merge your <branch A>
to <branch B>
, git will automatically commit a message saying "merge branch <branch A>
into <branch B>
.
合并<branch A>
到<branch B>
.git后,git 会自动提交一条消息,提示“将分支合并<branch A>
到<branch B>
.
If you want to customize git's merge commit message you can try:
如果您想自定义 git 的合并提交消息,您可以尝试:
$ git commit --amend -m "Your merge message"
This command will update your git's merge commit message to your commit message.
此命令会将您的 git 的合并提交消息更新为您的提交消息。
you can also try :
你也可以尝试:
$ git merge <branch A> --no-commit
it will merge your <branch B>
with <branch A>
, with list of <Branch B>'s
commit and commit messages
它会将您的<branch B>
with<branch A>
与<Branch B>'s
提交和提交消息列表 合并
If it fails to do fast-forward, then you will get something like this:
如果它不能快进,那么你会得到这样的东西:
Automatic merge went well; stopped before committing as requested
# check this with git status
$ git status
It will show you, your commits are already added to stage but not yet commited so, you can commit them without running git add
:
它会告诉您,您的提交已经添加到阶段但尚未提交,因此您可以在不运行的情况下提交它们git add
:
$ git commit -m "your merge commit message"
If you want to change <branch B>
's last commit message then again you can try:
如果您想更改<branch B>
的最后一次提交消息,您可以再次尝试:
$ git commit --amend -m "your new commit message"
But, generally, we don't update other commit messages, unless they are incorrect.
但是,通常我们不会更新其他提交消息,除非它们不正确。
Suppose, you get conflict after git merge, then simply resolve your conflict and do:
假设,您在 git merge 后遇到冲突,然后只需解决您的冲突并执行以下操作:
$ git add .
$ git commit -m "your commit message"
回答by Cascabel
There's also a --log
option for git-merge
now, which does half of what you want - it places the shortlog (commit summaries) in the merge message. A full solution will have to use a hook like in VonC's answer, though.
现在还有一个--log
选项git-merge
,它可以完成您想要的一半 - 它将短日志(提交摘要)放在合并消息中。不过,完整的解决方案必须使用像 VonC 的答案中那样的钩子。
回答by VonC
You could try defining a prepare-commit-msghook(the sample onedoes generate some custom "default commit messages")
您可以尝试定义一个prepare-commit-msg钩子(示例确实会生成一些自定义的“默认提交消息”)
回答by nilsM
A very simple bash function that set's a default message and adds your argument.
It opens your editor with the --edit
switch if you want make changes.
一个非常简单的 bash 函数,它设置默认消息并添加您的参数。--edit
如果您想进行更改,它会使用开关打开您的编辑器。
edit ~/.bashrc or bash_aliases. (Don't forget to source ~/.bashrc
) to apply changes in your bashrc
编辑 ~/.bashrc 或 bash_aliases。(不要忘记source ~/.bashrc
)在您的 bashrc 中应用更改
function mergedevelop()
{
git merge --no-ff --edit -m "master <-- develop: " develop;
}
use:
用:
mergedevelop "PR #143..."
to have message:
mergedevelop "PR #143..."
有消息:
master <-- develop: PR #143...
大师 <-- 开发:PR #143 ...
回答by Christian Severin
Coming late to the party, but nowadays we can just usegit merge --squash <branch>
to merge another branch into a single commit on my current branch and prepopulating our commit messagewith all the merged commit messages -- and the detailed messages, too, not just the one-liners.
I looked for ages for this command.
来晚了,但现在我们可以使用git merge --squash <branch>
将另一个分支合并到我当前分支上的单个提交中,并用所有合并的提交消息预先填充我们的提交消息——还有详细的消息,而不仅仅是单行代码.
我为这个命令寻找了很长时间。