如何将项目特定信息添加到 Git 提交注释中?
使用 Git,如果您要提交,它会在提交消息下包含一个被注释掉的部分。其中包含有关编写提交消息的说明以及正在更改的文件列表。像这样:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: important-file.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: some-other-thing.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# untracked.txt
是否可以向该文件添加更多材料?一个明确的用例是那些使用问题跟踪软件的用户,例如 Trac、Redmine 等指定额外的语法元素。例如,Redmine 用户可以包含问题编号和一些特殊关键字来将问题标记为已解决:关键字是“refs”(和“references”)和“fixes”(或“closes”) - 但我经常发现这很困难记住关键词。
那么,如果可以在提交指令的底部附加一些特定于项目的文本,那就会很方便。
有没有现有的方法可以做到这一点,或者我需要破解一种方法吗?作为一个附带问题,其他 VCS(例如 Mercurial)是否有类似的行为?
With Git, if you are committing, it includes a section under the commit message that is commented out. This contains instructions on writing a commit message as well as a list of files that are changing. Like this:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: important-file.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: some-other-thing.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# untracked.txt
Is it possible to add further material to this file? One clear use case is for those using issue tracking software like Trac, Redmine and the like is specifying extra syntactical elements. Redmine users, for instance, can include the issue number and some special keywords to mark issues as resolved: the keywords are "refs" (and "references") and "fixes" (or "closes") - but I often find it difficult to remember the keywords.
It would be handy, then, if one could append some project-specific text at the bottom of the commit instructions.
Is there any existing way of doing this or do I need to hack one in? As a side issue, do any other VCSes (like Mercurial) have similar behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我使用了 commit.template(如此处所述)。我本可以使用prepare-commit-msg钩子,但是提交模板可以使这项工作稍微容易一些,而且我可以将提交模板保留在项目的版本控制中。
I used a commit.template (as described here). I could have used a prepare-commit-msg hook, but a commit template does the job slightly easier, plus I can keep the commit template in the version control for the project.
选项 1:
正如其他人提到的,使用 Git 执行此操作的直接方法是 prepare-commit-消息 钩子。
然而,该策略存在一些必须考虑的问题。来自 Pro Git 第 7.4 章:
选项2:
如Tom Morris,使用提交模板。
选项 3:
您可以自定义构建 Git,其中包含附加说明。当前提交消息中包含的指令是硬编码在 Git 源代码中的。请参阅从
此方法可能不是首选方法,因为每次发布新版本的 Git 时都必须应用补丁。
选项 4:
为 Git 创建一个补丁,添加此功能并将其提交到邮件列表。如果您(或其他人)决定尝试此操作,我会首先从列表中寻求有关如何进行的建议。
Mercurial:
Mercurial 中的 Hook 脚本的行为方式类似。摘自《Mercurial:权威》的 第 10 章指导:
Option 1:
As others have mentioned, the direct way to do this with Git is the prepare-commit-message hook.
However, there are issues with that strategy that must be considered. From Chapter 7.4 of Pro Git:
Option 2:
As mentioned by Tom Morris, use a commit template.
Option 3:
You could do a custom build of Git which includes your additional instructions. The instructions included in the current commit message are hard-coded in the Git source code. See
$GIT_SRC/builtin/commit.c
starting at line 655.This method is probably not preferred since you would have to apply the patch every time a new version of Git is released.
Option 4:
Create a patch for Git which adds this feature and submit it to the mailing list. If you (or others) decide to try this, I would first ask for advice from the list on how to proceed.
Mercurial:
Hook scripts in Mercurial behave in a similar manner. From Chapter 10 of Mercurial: The Definitive Guide:
您可以通过
prepare-commit-message
hook。You can tweak the commit message via the
prepare-commit-message
hook.我很确定您可以为此使用prepare-commit-msg 挂钩。查看 githooks 了解更多信息。
I'm pretty sure you could use a prepare-commit-msg hook for this. Take a look at githooks for more information.