关于bash:从命令行向’git commit -m’添加换行符

Add line break to 'git commit -m' from the command line

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

这可能吗?


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

1
2
3
4
git commit -m 'Message

goes
here'

或者,您可以使用"here document"(也称为heredoc):

1
2
3
4
5
6
git commit -F- <<EOF
Message

goes
here
EOF


如果你只想要一条头条和一条内容,你可以使用:

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


使用Bash命令行中的Git,您可以执行以下操作:

1
2
3
4
git commit -m"this is
> a line
> with new lines
> maybe"

只需键入并按Enter,如果需要新行,">"符号表示您已按下Enter,并且有一个新行。其他答案也有效。


你应该可以使用

1
2
git commit -m $'first line
second line'

从Bash手册:

Words of the form $'string' are treated specially. The word expands to
string, with backslash-escaped characters replaced as specified by the
ANSI C standard.

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


在Git提交中添加换行符

请尝试以下操作以创建多行提交消息:

1
2
3
4
5
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."

然后验证你做了什么:

1
git log -1

你应该得到这样的东西:

Multi-line Git commit message in PowerShell

截图来自我使用PowerShell和Poshgit设置的示例。


做点什么

1
2
git commit -m"test
test"

不起作用,但有点像

1
2
git commit -m"$(echo -e"test
test")"

有效,但不是很漂亮。在PATH中设置git-commitlb命令,它执行以下操作:

1
2
3
4
5
#!/bin/bash

message=$1

git commit -m"$(echo -e"$message")"

并像这样使用它:

1
2
3
git commitlb"line1
line2
line3"

警告的话,我有一种感觉,即一般惯例是将摘要行作为第一行,然后是两个换行符,然后是提交消息中的扩展消息,因此这样做会破坏该约定。你当然可以这样做:

1
2
3
4
git commitlb"line1

line2
line3"


从Git文档:

-m
--message=
Use the given as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.

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

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


我希望这不会过于远离发布的问题,而是设置默认编辑器然后使用

1
git commit -e

可能会更舒服。


没有必要使这些东西复杂化。在-m"text...之后,按Enter获取下一行。按下Enter时出现>。完成后,只需输入"并按Enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ 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 ...


我在Mac上使用zsh,我可以在双引号(")中发布多行提交消息。基本上我一直在键入并按回新行,但是在我关闭引号并返回之前,消息不会发送到Git 。


在Bash / Zsh中,您只需在引号内使用文字换行符:

1
2
3
git commit -m 'Multi-line
commit
message'

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

git commit -m $'Multi-line
commit
message'

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

The editor used to edit the commit log message will be chosen from the
GIT_EDITOR environment variable, the core.editor configuration
variable, the VISUAL environment variable, or the EDITOR
environment variable (in that order). See git-var for details.


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

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

这就是我在.bashrc中所拥有的:

1
2
3
4
export ALTERNATE_EDITOR=''
export EDITOR='emacsclient -t'
export VISUAL='emacsclient -c'
export SUDO_EDITOR='emacsclient -t'


就个人而言,我发现在vi(或者你选择的git编辑器)之后修改提交消息最??简单,而不是在命令行上,通过在git commit之后执行git commit --amend


可悲的是,git似乎不允许在其消息中添加任何换行符。 上面已经有各种合理的解决方案,但是当编写脚本时,这些解决方案很烦人。 这里的文档也可以工作,但也可能有点太讨厌处理(想想yaml文件)

这是我做的:

1
2
3
git commit \
    --message"Subject" \
    --message"First line$(echo)Second line$(echo)Third Line"

虽然这仍然是丑陋的,但它允许"单行",这可能仍然有用。 由于通常字符串是变量或与变量组合,因此可以将uglynes保持在最小值。


以下是Windows上带有标准cmd.exe shell的失败解决方案列表(为了节省一些试错时间!):

  • git commit -m 'Hello Enter不起作用:它不会要求换行

  • git commit -m"Hello Enter同上

  • git commit -m"Hello^ Enter同上

  • git commit -m 'Hello^ Enter World'看起来像是在工作,因为它问"更多?"并允许写一个新行,但最后在做git log时你会看到它仍然是一行的消息......

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

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