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

zed*_*doo 48 git git-commit

是否可以修改默认git提交消息的注释部分?我想为我的用户添加更多"上下文"信息.

# 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
#
Run Code Online (Sandbox Code Playgroud)

Jak*_*ski 73

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

指定要用作新提交消息模板的文件." ~/"扩展为$ HOME和" ~user/" 的值到指定用户的主目录.

您可以将它放在per-repository(.git/config),user(~/.gitconfig)和system(/etc/gitconfig)配置文件中.

  • 这只会更改提交文本,即评论标记之前出现的内容.OP要求更改提交注释标记本身. (7认同)
  • 这真的很有帮助.有几行我不得不输入*每个*提交我.太好了!此外,我发现这个vim提示非常有用:[**将光标定位在提交消息的第一行**](http://vim.wikia.com/wiki/Always_start_on_first_line_of_git_commit_message).快乐的时光 ! (3认同)
  • 有趣的是,这个答案并没有回答原来的问题,尽管原来的问题可能已经解决了,但这个答案却得到了最多的支持!所以在我看来,这就是人们真正在寻找的东西(就像我自己一样)。 (2认同)

wei*_*ure 45

你可以使用git hooks.在提交更改的人员显示提交消息之前,将运行prepare-commit-msg脚本.

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

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

#!/bin/sh
echo "#Some more info...." >> $1
Run Code Online (Sandbox Code Playgroud)

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

  • commit.template配置变量正是出于此目的,应该使用它. (15认同)
  • 请注意钩子不会被添加到 repo 中。当这个 repo 被克隆到其他地方时,你必须再次设置钩子。 (2认同)