我正在从命令行使用Git,并试图在提交消息中添加换行符(使用Git commit -m "")而不进入Vim。

这可能吗?


当前回答

从命令行使用Git和Bash,你可以做到以下几点:

git commit -m "this is
> a line
> with new lines
> maybe"

当你想要新的一行时,只需输入并按Enter,“>”符号意味着你已经按了Enter,并且有一个新的行。其他答案也适用。

其他回答

如果你只是想要一个标题行和一个内容行,你可以使用:

git commit -m "My head line" -m "My content line."

注意,这会创建单独的段落,而不是行。所以在每两个-m行之间会有一个空行,例如:

My head line

My content line.

在Git提交中添加换行符

尝试以下命令创建多行提交消息:

git commit -m "Demonstrate multi-line commit message in Powershell" -m "Add a title to your commit after -m enclosed in quotes,
then add the body of your comment after a second -m.
Press ENTER before closing the quotes to add a line break.
Repeat as needed.
Then close the quotes and hit ENTER twice to apply the commit."

然后验证你做了什么:

git log -1

你应该得到这样的结果:

截图来自我使用PowerShell和Poshgit创建的一个示例。

这适用于所有终端和操作系统AFAIK。

下面是bash的示例:

导致这个提交:

来自Git文档:

- m <味精> ——消息= <味精> 使用给定的<msg>作为提交消息。如果给出了多个-m选项,它们的值将作为单独的段落连接起来。

所以,如果你正在寻找分组多个提交消息,这应该做的工作:

git commit -m "commit message1" -m "commit message2"

你应该能够使用

git commit -m $'first line\nsecond line'

从Bash手册:

形式为$'string'的单词被特殊处理。这个词扩展为 属性指定的反斜杠转义字符替换为 ANSI C标准。

这包括如上所示的对换行符的支持,以及十六进制和Unicode代码等。转到链接部分查看反斜杠转义字符的列表。

如果您使用Bash,点击C-x C-e (Ctrl+x Ctrl+e),它将在您首选的编辑器中打开当前命令。

您可以通过调整VISUAL和editor来更改首选编辑器。

这就是我的。bashrc文件:

export ALTERNATE_EDITOR=''
export EDITOR='emacsclient -t'
export VISUAL='emacsclient -c'
export SUDO_EDITOR='emacsclient -t'