git commit -m ""
),而不进入 Vim。这是否可能?当然,如何完成取决于你的shell。在Bash中,您可以在消息周围使用单引号,然后将引号留空,这将使Bash提示您输入另一行,直到您关闭引号。就像这样:
git commit -m 'Message
goes
here'
或者,您可以使用“here document”(也称为heredoc):
git commit -F- <<EOF
Message
goes
here
EOF
If you just want, say, a head line and a content line, you can use:
git commit -m "My head line" -m "My content line."
请注意,这将创建单独的段落-而不是行。 因此,在每两个-m
行之间会有一行空行,例如:My head line
My content line.
man git commit
中翻译得到:
-m <msg>, --message=<msg>
使用给定的 <msg> 作为提交信息。如果提供多个 -m 选项,则它们的值将作为单独段落连接在一起。 - Amedee Van Gasse在Bash命令行中使用Git,您可以执行以下操作:
git commit -m "this is
> a line
> with new lines
> maybe"
只需输入并按下Enter,即可在需要新行时创建一个新行,">"符号表示您已按下Enter键,并且有一个新行。其他答案也可以。
给你的 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 设置的一个示例。
据我所知,这适用于所有终端和操作系统。
这里有一个 bash 的示例:
生成如下提交:
git commit -m $'first line\nsecond line'
从Bash手册中得知:
表达式形如$'string'的字符串会被特殊对待。这种字符串会扩展为string,其中反斜杠转义字符将被替换为符合 ANSI C 标准的指定字符。
上述内容包括支持换行符,以及十六进制和Unicode编码等。请前往链接的部分查看反斜杠转义字符列表。
echo $'one\ntwo'
时,你看到了什么? - Dennis Williamsongit commit -m "first line"$'\n'"second line"
。请注意,在开始 $'string'
之前,您必须关闭先前的字符串。 - PlasmaBinturong没有必要使事情变得复杂。在 -m "text...
后的下一行是通过按下 Enter 获得的。当按下 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 ...
[2021年5月5日更新]
对于Windows用户,请使用GitBash for Windows。内置的Windows cmd
无法与此方法配合使用。
来自Git文档:
-m <msg>
--message=<msg>
使用给定的<msg>作为提交信息。如果提供了多个-m
选项,则它们的值将作为单独的段落连接。
因此,如果您要对多个提交消息进行分组处理,这应该可以解决问题:
git commit -m "commit message1" -m "commit message2"
执行类似以下的操作:
git commit -m"test\ntest"
无法工作,但类似于
git commit -m"$(echo -e "test\ntest")"
这样做是有效的,但不太美观。您可以在PATH
中设置一个git-commitlb
命令,类似于以下操作:
#!/bin/bash
message=$1
git commit -m"$(echo -e "$message")"
然后像这样使用:
git commitlb "line1\nline2\nline3"
提醒一句,我觉得一般的约定是在提交信息中将摘要行放到第一行,然后插入两个换行符,再加上详细的描述信息,因此这样做会打破这个约定。当然,你可以这样做:
git commitlb "line1\n\nline2\nline3"
git commit -m 'Multi-line
commit
message'
ANSI-C引用也适用于Bash/Zsh:
git commit -m $'多行\n提交\n信息'
你还可以指示Git使用你选择的编辑器来编辑提交消息。参见git-commit中的文档:
用于编辑提交日志消息的编辑器将从环境变量
GIT_EDITOR
、配置变量core.editor
、环境变量VISUAL
或环境变量EDITOR
(按此顺序)中选择。有关详细信息,请参见git-var。
因此,例如,要使用nano
编辑你的消息,可以运行:
export GIT_EDITOR=nano
git commit
GIT_EDITOR="emacs" git commit -m '段落1' -m '段落2' -e
这样的命令可以帮助你避免使用vim
。 - jotik