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

这可能吗?


当前回答

我在Mac上使用zsh,我可以在双引号(")内发布多行提交消息。基本上,我一直键入并按回车键获取新行,但直到我关闭引号并返回,消息才被发送到Git。

其他回答

在Bash/Zsh中,你可以简单地在引号内使用文字换行:

git commit -m 'Multi-line
commit
message'

ANSI-C引用也适用于Bash/Zsh:

git commit -m $' multiline \ncommit\nmessage'

您还可以指示Git使用您选择的编辑器来编辑提交消息。来自git-commit的文档:

用于编辑提交日志消息的编辑器将从 GIT_EDITOR环境变量,核心。编辑器配置 变量、VISUAL环境变量或编辑器 环境变量(按此顺序)。详见git-var。

因此,使用nano编辑您的消息,例如,您可以运行:

export GIT_EDITOR=nano
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不适用此方法。

当然,它是如何完成的取决于您的shell。在Bash中,可以在消息周围使用单引号,也可以只打开引号,这将使Bash提示另一行,直到关闭引号。是这样的:

git commit -m 'Message

goes
here'

或者,你可以使用“here文档”(也称为heredoc):

git commit -F- <<EOF
Message

goes
here
EOF

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

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

不需要更复杂的方法了。

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

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

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

My head line

My content line.