如何将 git commit 消息分成多行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29933349/
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
How can I make git commit messages divide into multiple lines?
提问by fernando
When I use git log
to check out my commit explanatory note I want it to look like this:
当我git log
用来查看我的提交说明时,我希望它看起来像这样:
1. what I changed
2. blank line
3. why I changed it
...being in 3 lines not 1 like this:
...在 3 行中,而不是像这样的 1 行:
1. what i changed 2. blank line 3. why i changed
However, git log
shows it in one line. So, how do I get this to happen using git commit -m
?
但是,git log
在一行中显示它。那么,我如何使用git commit -m
?
回答by Alex Pan
You just use the following command:
您只需使用以下命令:
$ git commit -m "1. what i changed
> 2. blank line
> 3. why i changed"
In your terminal, just hit 'enter' for a new line. The commit message won't end until you add the closing quote. The git log will look like:
在您的终端中,只需按“输入”即可换行。在您添加结束引号之前,提交消息不会结束。git 日志将如下所示:
$ git log
commit abcde2f660c707br2d20411581c4183170c3p0c2
Author: Alex Pan <[email protected]>
Date: Tue Apr 28 20:52:44 2015 -0700
1. what i changed
2. blank line
3. why i changed
回答by olegtaranenko
Excerpt from documentation
文档摘录
-m <msg> --message=<msg>
Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.
-m <msg> --message=<msg>
使用给定作为提交消息。如果给出了多个 -m 选项,它们的值将连接为单独的段落。
In your case it does exactly what you want, inserts a blank line between first and second lines
在您的情况下,它完全符合您的要求,在第一行和第二行之间插入一个空行
git commit -m "what I changed" -m "why I changed it"
This is useful if you want to amend previously added comment
如果您想修改以前添加的评论,这很有用
回答by Matthieu Moy
The multiple-line format you describe is the recommended one with Git (See DISCUSSION in the documentation of git commit). The simplest way to do it is to use git commit
without -m
, and write your message in your text editor.
您描述的多行格式是 Git 推荐的格式(请参阅git commit 文档中的讨论)。最简单的方法是使用git commit
without -m
,并在文本编辑器中编写您的消息。
回答by yaronyogev
I find it much easier to save the commit message to a file, and then use the -F option.
我发现将提交消息保存到文件中,然后使用 -F 选项要容易得多。
Example:
例子:
$ cat > /tmp/commit_msg.txt
DE123 bug fix: incorrect fetching of instance details
- fixed this and that
- also did such and such
$ git commit -F /tmp/commit_msg.txt
You could also use an editor to edit the message file before the commit.
您还可以使用编辑器在提交之前编辑消息文件。
回答by Tony Brix
Rather than use a temp file when trying to do this programmatically you can use stdin
尝试以编程方式执行此操作时,您可以使用 stdin,而不是使用临时文件
git commit -F-
git commit -F-
then write the message to stdin
然后将消息写入标准输入
回答by Gabriel Staples
I needed to have a bash script do multi-line git commits for me, so here's two options I came up with:
我需要一个 bash 脚本为我做多行 git 提交,所以我想出了两个选项:
Write to a temporary file then commit with the contents of the file as the message:
printf "first line\nsecond line\nthird line" > "file.txt" git commit -F "file.txt"
(My preferred approach): Write to a temporary variable then commit with the contents of the variable as the message. Note that the quotes around
$MSG
when doing any command to recall the contents of the variable are required!Without them, you'll lose your newlines.MSG="$(printf "first line\nsecond line\nthird line")" git commit -m "$MSG"
As an extension of this 2nd approach, in case you need to script building the message in multiple pieces or steps, that is possible too. WATCH OUT though! Notice where I place my newline (
\n
) characters. I do NOT place them at the end of anyprintf
string. That's because if I do they will get gobbled up, because bash automatically removes any trailingnewline characters, since it's dumb like that. So, do it like this instead, which works just fine:MSG="$(printf "first line")" MSG="$(printf "${MSG}\nsecond line")" MSG="$(printf "${MSG}\nthird line")" git commit -m "$MSG"
写入临时文件,然后将文件内容作为消息提交:
printf "first line\nsecond line\nthird line" > "file.txt" git commit -F "file.txt"
(我的首选方法):写入一个临时变量,然后将变量的内容作为消息提交。请注意,在执行任何命令以调用变量内容时,周围的引号
$MSG
是必需的!没有它们,您将丢失换行符。MSG="$(printf "first line\nsecond line\nthird line")" git commit -m "$MSG"
作为第二种方法的扩展,如果您需要在多个部分或步骤中编写构建消息的脚本,这也是可能的。不过要小心!请注意我放置换行符 (
\n
) 字符的位置。我不会将它们放在任何printf
字符串的末尾。那是因为如果我这样做,它们会被吞噬,因为 bash 会自动删除任何尾随的换行符,因为它就像那样愚蠢。所以,改为这样做,效果很好:MSG="$(printf "first line")" MSG="$(printf "${MSG}\nsecond line")" MSG="$(printf "${MSG}\nthird line")" git commit -m "$MSG"
Sample git log
output from any of the above git commit
s:
git log
来自上述任何一个的示例输出git commit
:
commit e1983659c6ae2e9d2eb4332657329837582fc32b (HEAD -> master)
Author: Gabriel Staples <[email protected]>
Date: Tue Mar 24 00:55:31 2020 -0700
first line
second line
third line