因此,我的分支以 bugtracker 票号命名,例如“issue-1234”,并且我们有一个约定,即始终在提交消息中写下票号。我想知道当我在 issues-* 分支上工作时是否可以自动将票号附加到提交消息中,而无需我显式键入它。
我查看了 git commit hooks,即预提交、准备消息和后提交,它们似乎都不能做我想要的事情。提交后挂钩已接近,但您无法修改使用 -m 提交的消息。
重申一下,我想知道这是否可能:
在分支上:issue-1234
git commit -a -m "fixed this pesky issue"
提交后,在 git log 中,它显示的消息为:
fixed this pesky issue. ticket number: #1234
So my branch is named after bugtracker ticket number, something like "issue-1234", and we have a convention to always write down ticket number in commit message. I'm wondering if it's possible to append the ticket number in commit message automatically when I'm working on an issue-* branch without me explicitly typing it.
I looked at git commit hooks, namely pre-commit, prepare-message, and post-commit, and none of them seem to be able to do what I wanted. Post-commit hook comes close, but you cannot modify the message that's committed with -m.
To reiterate, I'm wondering if this is possible:
On branch: issue-1234
git commit -a -m "fixed this pesky issue"
After the commit, in git log, it shows the message as:
fixed this pesky issue. ticket number: #1234
发布评论
评论(7)
你错过了一个钩子。您想要的是
commit-msg
:例如:
这是对分支名称的非常幼稚的解析,它只是附加到自己的行上的提交消息中。如果这对您来说还不够好,请修改它。
当然,我实际上建议在
prepare-commit-msg
中执行此操作,并使用git commit
提交(不带-m
)。您实际上可以在单行提交消息中写入足够的信息,这是非常非常罕见的。此外,这将让您在提交之前看到消息,以防您的钩子没有达到您想要的效果。You missed a hook. The one you want is
commit-msg
:So for example:
That's a very naive parsing of your branch name, and it's simply appended to the commit message on its own line. Modify it if that's not good enough for you.
Of course, I'd actually recommend doing this in
prepare-commit-msg
, and committing withgit commit
(without-m
). It's very, very rare that you can actually write sufficient information in a single-line commit message. Further, that will let you see the message before the commit is made, in case your hook doesn't do quite what you want.您还可以使用
prepare-commit-msg
钩子,它比commit-msg
接受更多的参数。然后,您可以检查消息是否来自文件、模板等,以避免在不需要时附加问题编号。当您在名为
foo-123
的功能分支中工作时,使用.git/hooks/prepare-commit-msg
中的以下脚本,然后[#123]
将被添加到您所做的每个提交的第三行。更多信息在我写的这篇文章中
You can as well use
prepare-commit-msg
hook, which accepts more parameters thancommit-msg
. Then you can check if the message is coming from a file, a template, etc to avoid appending the issue numbers when you don't want it.With the following script in
.git/hooks/prepare-commit-msg
when you are working in a feature branch namedfoo-123
, then[#123]
will be added to the third line of every commit you make.More information in this post I wrote
这样您就可以将分支名称添加到提交消息的开头。这是准备提交消息钩子。
适用于“git commit -m”和“git commit”命令。该选项是文件 .git/hooks/pre-commit.skip,其中包含您不想自动添加的分支列表。
This way you can add branch name to the start of commit message. It's prepare-commit-msg hook.
Work both for "git commit -m" and "git commit" commands. The option is file .git/hooks/pre-commit.skip which contains a list of branches you don't want to auto-prepend.
使用 预提交 以及 预提交 。 com/milin/giticket" rel="nofollow noreferrer">giticket 钩子,可以很好地自动在提交中包含票号。
Using pre-commit along with the giticket hook, works pretty well to have the ticket number in the commit automatically.
这是针对任何类型的问题/票据编号提交消息的完整解决方案:
prepare-commit-msg
将其放入存储库的
.git/hooks
目录中以仅应用于存储库,或设置 ~/.gitconfig 中的“https://git-scm.com/docs/git-config#git-config-corehooksPath” rel="nofollow noreferrer">core.hooksPath 和复制到该目录以应用到您的所有存储库。请参阅 我的配置文件存储库< /a> 除了其他有用的脚本之外。
Here's a complete solution for any kind of issue/ticket numbering commit messages:
prepare-commit-msg
Put it into repository's
.git/hooks
directory to apply only to the repo, or set up core.hooksPath in~/.gitconfig
and copy to that directory to apply to all of your repositories.See in my config files repository besides other useful scripts.
另一种选择是使用
git注释
使用您提到的挂钩之一将票号信息添加到提交中。(有关注释机制的更多信息,请参阅“自我注释”博客文章条目)
Another option would be to use
git notes
to add the ticket number information to the commit, using one of the hooks you mention.(See "Notes to self" blog post entry for more on the notes mechanism)
因为这对于寻求快速解决方案的人来说可能很有用 - 具有改进的可能性和相当好的可移植性(将其添加到新框中只需简单的 bash
source git-tricks.sh
即可)我们的分支名称通常位于形式:
/-
就像:
bug/ID-1234-bad-button-color
然后我有以下别名:
alias git-branch-name='git rev-parse --abbrev-ref HEAD'
输出:
bug/ID-1234-bad-button-color
alias git-branch-ticket='git-branch-name | grep -oP "^[^/]*/\K[^-]*-[0-9]+"'
输出:
ID-1234
(如果是问题的作者,则应该是:
'git-branch-name | grep -oP "^issue-\K[0-9]+"'
)最后一个:
alias git-describe-commit='git commit --all --edit --message "[$(git-branch-ticket)] --edit this--"'
这允许我使用
git-描述-提交
用于快速添加对存储库的更改。As this maybe useful for somebody looking for quick solution - with possibility of improvement and quite good portability (adding this to a new box is matter of simple bash
source git-tricks.sh
)Our branchnames are usually in the form:
<work-category>/<ticket-id>-<short-description>
Like:
bug/ID-1234-bad-button-color
I then have following aliases:
alias git-branch-name='git rev-parse --abbrev-ref HEAD'
Output:
bug/ID-1234-bad-button-color
alias git-branch-ticket='git-branch-name | grep -oP "^[^/]*/\K[^-]*-[0-9]+"'
Output:
ID-1234
(In case of author of the question it should be:
'git-branch-name | grep -oP "^issue-\K[0-9]+"'
)And final one:
alias git-describe-commit='git commit --all --edit --message "[$(git-branch-ticket)] --edit this--"'
Which allows me to use
git-describe-commit
for quickly adding changes to the repo.