如何更改git commit消息中的默认注释?
是否可以修改默认git commit消息的注释部分?我想为我的用户添加更多的“上下文”信息。
# Please enter the commit message for your changes.
# (Comment lines starting with '#' will not be included)
# Explicit paths specified without -i nor -o; assuming --only paths...
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: test.txt
#
zedoo asked 2020-02-13T04:37:40Z
3个解决方案
72 votes
有.git/config
配置变量,根据git-config(1)联机帮助页:
指定一个文件用作新提交消息的模板。 将“
.git/config
”扩展到$ HOME的值,将“~/.gitconfig
”扩展到指定用户的主目录。
您可以将其放入每个存储库(.git/config
),用户(~/.gitconfig
)和系统(/etc/gitconfig
)配置文件中。
45 votes
您可以为此使用git hooks。 在向要提交更改的人显示提交消息之前,运行prepare-commit-msg脚本。
您可以在.git / hooks中找到示例prepare-commit-msg脚本。
要编辑默认消息,请在.git / hooks文件夹中创建一个名为prepare-commit-msg的新文件。 您可以使用以下脚本来编辑提交消息:
#!/bin/sh
echo "#Some more info...." >> $1
$ 1变量存储提交消息文件的文件路径。
0 votes
这是一个python git-hook,用于清理默认消息。 挂钩名称:prepare-commit-msg
#!/usr/bin/env python
import sys
commit_msg_file_path = sys.argv[1]
with open(commit_msg_file_path, 'a') as file:
file.write('')
您只需在file.write()
方法中添加文本即可。