是否可以修改默认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
#
有帮助吗?

解决方案

您可以使用 git钩 为了那个原因。在要提交更改的人显示提交消息之前,正在运行准备命令-MSG脚本。

您可以在.git/hooks中找到一个示例准备commit-msg脚本。

要编辑默认消息,请在.git/hooks文件夹中创建一个名为preparimmit-msg的新文件。您可以使用这样的脚本编辑提交消息:

#!/bin/sh
echo "#Some more info...." >> $1

$ 1变量将文件路径存储到提交消息文件。

其他提示

commit.template 配置变量,根据 git-config(1) manpage:

指定要使用的文件 新提交消息的模板. "~/“扩展到$ home的价值”和~user/“到指定的用户主目录。

您可以将其放入每个重复项(.git/config),用户(~/.gitconfig)和系统(/etc/gitconfig)配置文件。

这里有一个 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() 方法。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top