如何更改 git commit 消息中的默认注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3966714/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:18:56  来源:igfitidea点击:

How can I change the default comments in the git commit message?

gitgit-commit

提问by zedoo

Is it possible to modify the commented part of the default git commit message? I want to add a bit more 'context' information for my users.

是否可以修改默认 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
#

采纳答案by weiqure

You can use git hooksfor that. Before the person who wants to commit the changes is shown the commit message, the prepare-commit-msg script is run.

您可以为此使用git hooks。在要提交更改的人显示提交消息之前,会运行 prepare-commit-msg 脚本。

You can find an example prepare-commit-msg script in .git/hooks.

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

To edit the default message create a new file called prepare-commit-msg in the .git/hooks folder. You can edit the commit message by using a script like this:

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

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

The $1 variable stores the file path to the commit message file.

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

回答by Jakub Nar?bski

There is commit.templateconfiguration variable, which according to git-config(1)manpage:

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

Specify a file to use as the template for new commit messages. "~/" is expanded to the value of $HOME and "~user/" to the specified user's home directory.

指定一个文件作为新提交消息模板。" ~/" 扩展为 $HOME 的值," " 扩展为~user/指定用户的主目录。

You can put it in per-repository (.git/config), user's (~/.gitconfig) and system (/etc/gitconfig) configuration file(s).

您可以将它放在每个存储库 ( .git/config)、用户 ( ~/.gitconfig) 和系统 ( /etc/gitconfig) 配置文件中。

回答by swayamraina

Here is a python git-hookto clean up the default message. Hook name: prepare-commit-msg.

这是一个用于清理默认消息的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('')

You can simply add you text in the file.write()method.

您可以简单地在file.write()方法中添加文本。