如何自定义 git 的合并提交消息?
每次进行合并时,我都需要生成合并提交,并且我希望它不仅仅是所有提交的摘要。
我的问题是如何格式化 git-fmt-merge-msg 或决定此自动消息的因素(我可以在提交后通过修改它并使用 git-log --pretty=format 手动执行此操作: '...')
例如我想将其格式化为这样:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我知道这并没有回答最初的问题,但为了像我这样到达此页面的 git 菜鸟的利益,因为它目前是“git Change merge commit message”的第一个 Google 结果,我会提到这是可能的to:
更改合并提交的提交消息,而不丢失与合并提交的任何父级的链接。
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:
to change the commit message of a merge commit without losing the link to any of the parents of the merge commit.
看起来从版本 Git 1.7.8 开始,您可以执行
git merge --edit ...
指定提交消息。
从 1.7.10 开始,进入编辑模式将是默认行为
(尽管我没有在 Windows 上的 msysgit 中看到它)。
Looks like as of version Git 1.7.8 you can do
git merge --edit ...
to specify the commit message.
And as of 1.7.10, dropping into edit mode will be the default behavior
(though I'm not seeing it on in msysgit on windows).
我想做这样的事情。我没有找到任何合理的方法来让 git fmt-merge-msg 工作。我认为它没有按照我希望的方式工作(传递一个完全自定义的文本用于消息)。因此,我想出了另一种方法,使用
-no-commit
和commit -F
命令。输出当然是可定制的,但它几乎完全反映了您所说的您想要的输出。示例提交消息输出:
用法是:
我将在此处复制别名:
如果您想复制并粘贴它来自定义它,请使用上面的内容。下面的版本有您不想要的换行符,但我将用它来解释我在做什么:
好吧...
第 1 行 将自定义函数作为 bash shell 脚本启动,以便 git 知道它是不是 git 命令。它将当前分支(如果要将不同的分支合并到 master 中,则为 master)设置为一个变量,以便我们稍后可以使用它。
第 2 行 使用当前分支和您为原始命令指定的分支名称打印第一行(就像在普通合并命令中一样)。它将其写入临时文件。
第 3 行获取传入分支中不在当前分支中的提交的日志,并仅将这些提交的主题写入临时文件。
第 4 行将下一行打印到 temp。
第 5 行获取当前分支中不在传入分支中的提交日志,并仅将这些提交的主题写入临时文件。
第 6 行在摘要和详细信息部分之间打印一个水平分隔符。
第 7 行获取当前分支和传入分支中所有提交的日志,直到它们彼此分支或最后共享祖先之后的时间。左右给出了一个箭头,显示提交来自哪个分支。 <表示当前分支并且>表示传入分支。
第 8 行 对传入分支执行合并命令,不带快进(因此您获得提交),也不带提交(因此您必须自己编写一个...啊,但您不这样做!)
第 9 行 使用
-e
和-F
参数执行提交命令,以允许编辑并告诉提交使用以下内容填充消息指定文件中的文本。一旦您根据需要完成提交消息,它就会提交合并并删除临时文件。田田!该长命令末尾的两个
;
使 printf 函数不会写入控制台,而只会写入文件。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
andcommit -F
commands. The output is of course customizable but it reflects almost exactly what you said you wanted the output to be.Sample Commit Message Output:
And the usage would be:
I'll copy the alias here:
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:
Alright...
Line 1 starts 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 2 prints 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 3 gets 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 4 prints the next line to temp.
Line 5 gets 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 6 prints a little horizontal separator between the summary and detail portions.
Line 7 gets 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 8 executes 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 9 executes 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.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.我发现有两种方法可以解决这个问题
注意:不要同时使用这两种方法,因为如果提交合并失败,它会再次将日志添加到底部。
个人说明:我使用第一个解决方案,因为它完全依赖于 git 的钩子和配置属性,而不是外部脚本。
对于真正的解决方案,必须扩展名为“fmt-merge-msg”的 git 命令,该命令在传递 --log 选项时生成单行描述(如果您确实需要此解决方案,则必须创建自己的补丁(对于git)并从源代码编译)。
1.使用prepare-commit-message作为VonC建议
此解决方案存在的问题是,您需要中断提交,然后手动提交
设置将构建所需提交消息的别名:
通过在 $GIT_DIR/hooks/ 中创建可执行的prepare-commit-msg 来创建prepare-commit-msg 钩子(下面的示例脚本)
应该定义一个别名提交消息,例如
2。使用将自动生成合并的自定义命令
(使用在 1 中创建的 lm 别名。)
然后执行一个相当严格的命令:
如果您仍然希望获得提交的单行描述,您需要向 -m 参数添加新命令或其他内容(如果您使用 - -log 那么它会在底部生成)
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).
1. using prepare-commit-message as VonC suggested
this solution has the problem that you need to interrupt the commit and then commit manually
setting an alias that will build the desired commit message:
creating the prepare-commit-msg hook by creating an executable prepare-commit-msg in $GIT_DIR/hooks/ (example script below)
one should define an alias commit msg such as
2. using a custom command that will generate the merge automatically
(using the lm alias created in 1.)
and then execute a rather rigid command:
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)
将
合并到
后,git 将自动提交一条消息“mergebranch
” 此如果您想自定义 git 的合并提交消息,您可以尝试:
命令会将 git 的合并提交消息更新为您的提交消息,
您也可以尝试:
它会 。将
与
合并,并包含's
提交列表和提交消息If它无法快进,然后你会得到类似这样的结果:
它会告诉你,你的提交已经添加到阶段但尚未提交,因此,你可以在不运行 git add 的情况下提交它们:
如果您想更改
的最后提交消息,那么您可以再次尝试:但是,通常,我们不会更新其他提交消息,除非它们不正确
假设您得到 。 git merge 后发生冲突,然后简单地解决冲突并执行以下操作:
Once you merge your
<branch A>
to<branch B>
, git will automatically commit a message saying "merge branch<branch A>
into<branch B>
.If you want to customize git's merge commit message you can try:
This command will update your git's merge commit message to your commit message.
you can also try :
it will merge your
<branch B>
with<branch A>
, with list of<Branch B>'s
commit and commit messagesIf it fails to do fast-forward, then you will get something like this:
It will show you, your commits are already added to stage but not yet commited so, you can commit them without running
git add
:If you want to change
<branch B>
's last commit message then again you can try: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
还有一个--log
选项,它可以完成您想要的一半工作 - 它将短日志(提交摘要)放在合并消息中。不过,完整的解决方案必须使用 VonC 的答案中的钩子。There's also a
--log
option forgit-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.您可以尝试定义 prepare-commit-msg< /strong> 钩子 (示例一确实生成了一些自定义的“默认提交消息”)
You could try defining a prepare-commit-msg hook (the sample one does generate some custom "default commit messages")
聚会迟到了,但现在我们可以使用
git merge --squash <分支>
将另一个分支合并到我当前分支上的单个提交并使用所有合并的提交消息预填充我们的提交消息 - 还有详细消息,而不仅仅是单行代码。
我找了很长时间才找到这个命令。
Coming late to the party, but nowadays we can just use
git merge --squash <branch>
to merge another branch into a single commit on my current branch and prepopulating our commit message with all the merged commit messages -- and the detailed messages, too, not just the one-liners.
I looked for ages for this command.
除了 Christian Severin 的 git merge --squash 之外="https://stackoverflow.com/a/50333256/6309">答案,您现在拥有配置
merge.suppressDest
作为自定义(一小部分) 提交消息。使用 Git 2.29(2020 年第 4 季度),“
git merge
“(man) 学会了使用merge.suppressDest
配置选择性地省略默认合并消息标题末尾的“into
”。请参阅提交 6e6029a(2020 年 7 月 29 日)和 提交 2153192(2020 年 7 月 30 日),作者: Junio C Hamano (
gitster
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 341a196,2020 年 8 月 1 日)git config
现在包含在其 手册页:和:
In addition of
git merge --squash
mentioned in Christian Severin's answer, you now have the configmerge.suppressDest
as a new way to customize a (small part of the) commit message.With Git 2.29 (Q4 2020), "
git merge
"(man) learned to selectively omit "into <branch>
" at the end of the title of default merge message withmerge.suppressDest
configuration.See commit 6e6029a (29 Jul 2020), and commit 2153192 (30 Jul 2020) by Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit 341a196, 01 Aug 2020)git config
now includes in its man page:And:
一个非常简单的 bash 函数,设置默认消息并添加您的参数。
如果您想进行更改,它会使用
--edit
开关打开您的编辑器。编辑 ~/.bashrc 或 bash_aliases。 (不要忘记
source ~/.bashrc
)在 bashrc 中应用更改,使用:
mergedevelop "PR #143..."
得到消息:master < -- 开发:PR #143...
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.edit ~/.bashrc or bash_aliases. (Don't forget to
source ~/.bashrc
) to apply changes in your bashrcuse:
mergedevelop "PR #143..."
to have message:master <-- develop: PR #143...
您可以使用以下命令在合并期间覆盖或自定义合并消息:
它已在 git 版本 2.34 上进行了测试,如果您安装了 git,则可以通过键入“git merge --help”在文档中找到它,或者在 git-merge 在回答这个问题时。
You can override or customize a merge message during the merge with the command below:
It was tested on git version 2.34, you can find it on the documentation by typing "git merge --help" if you have git installed, or at git-merge at the time of this answer.