我正在从命令行使用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"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"

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

只需要这样:

git commit

下面是在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可能是最好的选择。

我一直在寻找这个问题的答案,因为我曾经多次看到用于检查提交消息的正则表达式。无论如何,在使用-m标志创建提交时,没有任何想法可以工作。

因此,实现多行提交消息的最简单方法是在终端中只输入git commit。 然后,你将有机会输入你想要的笔记:

不需要更复杂的方法了。

没有必要把事情复杂化。在-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不适用此方法。