我正在从命令行使用Git,并试图在提交消息中添加换行符(使用Git commit -m "")而不进入Vim。
这可能吗?
我正在从命令行使用Git,并试图在提交消息中添加换行符(使用Git commit -m "")而不进入Vim。
这可能吗?
当前回答
没有必要把事情复杂化。在-m "文本之后…下一行是通过按Enter得到的。当按下回车键时,出现>。当你完成后,只需输入“”并按Enter:
$ git commit -m "Another way of demonstrating multicommit messages:
>
> This is a new line written
> This is another new line written
> This one is really awesome too and we can continue doing so till ..."
$ git log -1
commit 5474e383f2eda610be6211d8697ed1503400ee42 (HEAD -> test2)
Author: ************** <*********@gmail.com>
Date: Mon Oct 9 13:30:26 2017 +0200
Another way of demonstrating multicommit messages:
This is a new line written
This is another new line written
This one is really awesome too and we can continue doing so till ...
(编辑05-05-2021)
对于Windows用户,请使用GitBash For Windows。内置的Windows cmd不适用此方法。
其他回答
如果你只是想要一个标题行和一个内容行,你可以使用:
git commit -m "My head line" -m "My content line."
注意,这会创建单独的段落,而不是行。所以在每两个-m行之间会有一个空行,例如:
My head line
My content line.
你应该能够使用
git commit -m $'first line\nsecond line'
从Bash手册:
形式为$'string'的单词被特殊处理。这个词扩展为 属性指定的反斜杠转义字符替换为 ANSI C标准。
这包括如上所示的对换行符的支持,以及十六进制和Unicode代码等。转到链接部分查看反斜杠转义字符的列表。
我没有看到任何人提到如果你不提供消息,它会为你打开nano(至少在Linux中),在那里你可以写多行…
只需要这样:
git commit
来自Git文档:
- m <味精> ——消息= <味精> 使用给定的<msg>作为提交消息。如果给出了多个-m选项,它们的值将作为单独的段落连接起来。
所以,如果你正在寻找分组多个提交消息,这应该做的工作:
git commit -m "commit message1" -m "commit message2"
比如
git commit -m"test\ntest"
这并不奏效,但就像
git commit -m"$(echo -e "test\ntest")"
有用,但不是很漂亮。你在PATH中设置了一个git- commitb命令,它是这样做的:
#!/bin/bash
message=$1
git commit -m"$(echo -e "$message")"
像这样使用它:
git commitlb "line1\nline2\nline3"
提醒一下,我感觉一般惯例是第一行是摘要行,然后是两个换行符,然后是提交消息中的扩展消息,所以这样做会打破惯例。你当然可以:
git commitlb "line1\n\nline2\nline3"