使用井号 (#) 开始 git commit 消息

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

Start a git commit message with a hashmark (#)

gitcommitticket-systemticket-tracking

提问by knittl

Git treats lines starting with #as comment lines when committing. this is very annoying when working with a ticket tracking system, and trying to write the ticket number at the beginning of the line, e.g.

#提交时,Git 将开头的行视为注释行。这在使用票务跟踪系统时非常烦人,并试图在行的开头写下票号,例如

#123 salt hashed passwords

git will simply remove the line from the commit message. is there any way to escape the hash? i tried \and !, but nothing works. whitespaces before #are preserved, so they aren't a working solution to the problem either.

git 将简单地从提交消息中删除该行。有没有办法逃避哈希?我试过\!,但没有任何效果。保留之前#的空格,因此它们也不是问题的有效解决方案。

采纳答案by CB Bailey

This behaviour is part of git commit's default 'clean-up' behaviour. If you want to keep lines starting with #you can use an alternative clean-up mode.

此行为是git commit默认“清理”行为的一部分。如果你想保持行开始,#你可以使用另一种清理模式。

E.g.

例如

git commit --cleanup=whitespace

If you do this you have to be careful to remove all #lines that you don't want to appear in the commit.

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

回答by VonC

Note that, since git1.8.2 (February 2013), you can use a different character than '#' for the commented line in the commit message.

请注意,从git1.8.2 (February 2013) 开始,您可以#对提交消息中的注释行使用与 ' '不同的字符。

That allows you to use '#' for your bug number reference.

这允许您使用“ #”作为错误编号参考。

Various "hint" lines Git gives when it asks the user to edit messages in the editor are commented out with '#' by default.

The core.commentCharconfiguration variable can be used to customize this '#' to a different character.

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

所述core.commentChar配置变量可用于这种“自定义#”到一个不同的角色。



In theory, you couldput a core.commentCharword (multiple characters), but git 2.0.x/2.1 will be stricter (Q3 2014).

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

See commit 50b54fdby Nguy?n Thái Ng?c Duy (pclouds):

提交50b54fd通过Nguyñ泰国伍Ç维战(? )pclouds

config: be strict on core.commentChar

配置:对 core.commentChar 严格

We don't support comment strings(at least not yet). And multi-byte character encoding could also be misinterpreted.

The test with two commas is updated because it violates this. It's added with the patch that introduces core.commentCharin eff80a9(Allow custom "comment char" - 2013-01-16). It's not clear to me whythat behavior is wanted.

我们不支持注释字符串(至少现在不支持)。多字节字符编码也可能被误解。

带有两个逗号的测试被更新,因为它违反了这一点。它与eff80a9引入core.commentChar的补丁一起添加(允许自定义“评论字符” - 2013-01-16)。我不清楚为什么需要这种行为。



git 2.0.x/2.1 (Q3 2014) will add an automatic selection for core.commentChar:
See commit 84c9dc2

git 2.0.x/2.1 (Q3 2014) 将添加一个自动选择core.commentChar
提交 84c9dc2

When core.commentCharis "auto", the comment char starts with '#' as in default but if it's already in the prepared message, find another char in a small subset. This should stop surprises because git strips some lines unexpectedly.

Note that git is not smart enough to recognize '#' as the comment char in custom templates and convert it if the final comment char is different.
It thinks '#' lines in custom templates as part of the commit message. So don't use this with custom templates.

core.commentChar是 " auto" 时,注释字符#默认以 ' '开头,但如果它已经在准备好的消息中,请在小子集中找到另一个字符。这应该会停止意外,因为 git 意外地删除了一些行。

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

The list of candidate characters for "auto" are:

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

# ; @ ! $ % ^ & | :

That means a command like git commit -m '#1 fixed issue'will automatically switch the commentChar to ';', because '#' was used in the commit message.

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

回答by Matthieu Napoli

Answers here are good and detailed, but for a git noob like me customizing git config options isn't so obvious. Here is an example to change from #to ;for comment characters:

这里的答案很好而且很详细,但是对于像我这样的 git noob 来说,自定义 git 配置选项并不是那么明显。下面是一个#;注释字符从 更改为 的示例:

git config core.commentChar ";"

That's all you need to do.

这就是你需要做的。

回答by Olivier Verdier

You can use the command line option -m:

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

git commit -m "#123 fixed"

回答by Owain Williams

If you're doing an interactive rebase, then when you save your commit message with nothing in it (because the #at the beginning has made it a comment and therefore it's been ignored) git will show you what to do:

如果您正在执行交互式变基,那么当您保存没有任何内容的提交消息时(因为#开头已将其设为注释,因此已被忽略)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

So, just amend the message:

所以,只需修改消息:

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

and continue the rebase:

并继续变基:

git rebase --continue

回答by Sungam

git commit --cleanup=scissorsshould be used. It's added to Git v2.0.0on 2014.05.21

git commit --cleanup=scissors应该使用。它于 2014.05.21添加到Gitv2.0.0

from git commit --help

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.

回答by wilhelmtell

Use a different prefix for the ticket number. Or prepend a word to the ticket number, like "Bug #42". Or prepend a single space character to the line; if you wish to remove that whitespace you can add a commit-hook for that.

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

I personally would rather not have this sort of commit message manipulation done by a hook because it can be very irritating when it triggers when you don't want it to. The easiest solution is probably to re-think the problem.

我个人宁愿不要通过钩子来完成这种提交消息的操作,因为当它在你不想要的时候触发时会非常恼人。最简单的解决方案可能是重新思考问题。

回答by valignatev

All my commits starts with #issueNumberso I put this boilerplate to my vim .git/hooks/commit-msg:

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

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

So let's suppose we have branch #15and we make commit message add new awesome feature. With this approach final commit message will be #15 add new awesome feature.

所以让我们假设我们有分支#15并且我们制作 commit message add new awesome feature。使用这种方法,最终提交消息将是#15 add new awesome feature.