用hashmark(#)开始一个git commit消息

在提交时,Git把以#开始的行看作注释行。 在使用票据跟踪系统时,这是非常烦人的,并且试图在票号开头写上票号,例如,

 #123 salt hashed passwords 

git将简单地从提交消息中删除该行。 有没有办法摆脱哈希? 我试过\! ,但没有任何作品。 #之前的空格被保留,所以它们也不是解决问题的工作方法。

这个行为是git commit默认的“清理”行为的一部分。 如果要保持以#开头的行,则可以使用其他清理模式。

例如

 git commit --cleanup=whitespace 

如果你这样做,你必须小心删除所有你不想出现在提交中的#行。

请注意,自git1.8.2(2013年2月)以来,可以使用与“ # ”不同的字符作为提交消息中的注释行。

这允许你使用' # '作为你的bug编号参考。

Git在编辑器中要求用户编辑消息时提供的各种“提示”行默认情况下用“ # ”注释掉。

core.commentCharconfigurationvariables可用于将此“ # ”自定义为不同的字符。


理论上,你可以放一个core.commentChar单词(多个字符),但是git 2.0.x / 2.1会更严格(Q3 2014)。

见NguyễnTháiNgọcDuy( pclouds ) 提交的50b54fd :

configuration:在core.commentChar上严格

我们不支持评论string (至less现在还没有)。 多字节字符编码也可能被误解。

两个逗号的testing更新,因为它违反了这一点。 它添加了在core.commentChar中引入core.commentChar的补丁(允许自定义“comment char” – 2013-01-16)。 我不清楚为什么这种行为是想要的。


git 2.0.x / 2.1(Q3 2014)将为core.commentChar添加一个自动select:
见提交84c9dc2

core.commentChar为“ auto ”时,注释char以默认值“ # ”开头,但如果它已经在准备好的消息中,则在一个小子集中查找另一个char。 这应该停止惊喜,因为git意外地剥去了一些线。

请注意,git不够聪明,在自定义模板中将“ # ”识别为注释字符,如果最终的注释字符不同,则将其转换。
它认为自定义模板中的“#”行是提交消息的一部分。 所以不要在自定义模板中使用它。

“auto”的候选字符列表是:

 # ; @ ! $ % ^ & | : 

这意味着像git commit -m '#1 fixed issue'这样的命令会自动将commentChar切换为' ; ',因为在提交消息中使用了' # '。

您可以使用命令行选项-m

 git commit -m "#123 fixed" 

这里的答案是好的和详细的,但对于像我这样的git noob定制gitconfiguration选项并不那么明显。 这是一个从#改为的例子; 对于评论字符:

 git config core.commentChar ";" 

这就是你需要做的。

如果你正在做一个交互式的rebase,那么当你没有任何东西的时候保存你的提交信息(因为#开头已经把它作为一个注释,所以它被忽略了),git会告诉你该怎么做:

 Aborting commit due to empty commit message. Could not amend commit after successfully picking 5e9159d9ce3a5c3c87a4fb7932fda4e53c7891db... 123 salt hashed passwords This is most likely due to an empty commit message, or the pre-commit hook failed. If the pre-commit hook failed, you may need to resolve the issue before you are able to reword the commit. You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue 

所以,只需修改消息:

 git commit --amend -m "#123 salt hashed passwords" 

并继续转发:

 git rebase --continue 

git commit --cleanup=scissors应该被使用。 它在2014.05.21被添加到Git v2.0.0

git commit --help

 --cleanup=<mode> scissors Same as whitespace, except that everything from (and including) the line "# ------------------------ >8 ------------------------" is truncated if the message is to be edited. "#" can be customized with core.commentChar. 

为票号使用不同的前缀。 或在票号上加一个字,如“Bug#42”。 或者在行上添加一个空格字符; 如果你想删除空白,你可以添加一个提交钩子。

我个人宁愿不用钩子来完成这样的提交消息操作,因为当你不想要的时候会触发它,这可能是非常令人讨厌的。 最简单的解决scheme可能是重新考虑这个问题。

我所有的提交都以#issueNumber开头,所以我把这个样板放到我的vim .git/hooks/commit-msg

 NAME=$(git branch | grep '*' | sed 's/* //') echo "$NAME"' '$(cat "$1") > "$1" 

所以,让我们假设我们有分支#15 ,我们提交消息add new awesome feature 。 有了这个方法,最终的提交信息将是#15 add new awesome feature