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

这可能吗?


当前回答

下面是在Windows上使用标准cmd.exe shell的失败解决方案列表(为您节省一些试错时间!):

git commit -m 'Hello Enter不起作用:它不会要求新的行 git commit -m "Hello输入idem git commit -m "Hello^输入idem git commit -m 'Hello^ Enter World'看起来很正常,因为它会问"More?",并允许写新的一行,但最后当执行git log时,你会看到它仍然是一行消息…

TL;DR:即使在Windows上,命令行解析的工作方式不同,^允许多行输入,在这里也没有帮助。

最后,git commit -e可能是最好的选择。

其他回答

在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 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"

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

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

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

就我个人而言,我发现在vi(或任何你选择的git编辑器)中修改提交消息比在命令行中更容易,在git提交后立即执行git commit——modify。

我没有看到任何人提到如果你不提供消息,它会为你打开nano(至少在Linux中),在那里你可以写多行…

只需要这样:

git commit