我正在从命令行使用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不适用此方法。

其他回答

在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

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

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

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

My head line

My content line.

如果您使用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'

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

git commit -m 'Message

goes
here'

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

git commit -F- <<EOF
Message

goes
here
EOF

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

只需要这样:

git commit