我正在从命令行使用 Git,并试图在不进入 Vim 的情况下向提交消息添加换行符(使用 git commit -m ""
)。
这可能吗?
GIT_EDITOR="emacs" git commit -m 'paragraph1' -m 'paragraph2' -e
之类的内容可以帮助您避免使用 vim
。
当然,它是如何完成的取决于你的 shell。在 Bash 中,您可以在消息周围使用单引号,并且可以将引号保持打开状态,这将使 Bash 提示输入另一行,直到您关闭引号。像这样:
git commit -m 'Message
goes
here'
或者,您可以使用“此处文档”(也称为 heredoc):
git commit -F- <<EOF
Message
goes
here
EOF
如果你只想要一个标题行和一个内容行,你可以使用:
git commit -m "My head line" -m "My content line."
请注意,这会创建单独的段落 - 而不是行。所以每两个 -m
行之间会有一个空行,例如:
My head line
My content line.
-m
。好的!
man git commit
:-m <msg>, --message=<msg>使用给定的 <msg>作为提交消息。如果给出了多个 -m 选项,它们的值将连接为单独的段落。
-m
行之间会有一个空行。
使用带有 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
你应该得到这样的结果:
https://i.stack.imgur.com/Pgvhm.png
屏幕截图来自我使用 PowerShell 和 Poshgit 设置的示例。
这适用于所有终端和操作系统 AFAIK。
这是一个使用 bash 的示例:
https://i.stack.imgur.com/eRgFc.png
导致此提交:
https://i.stack.imgur.com/x7il3.png
你应该可以使用
git commit -m $'first line\nsecond line'
从 Bash manual:
$'string' 形式的单词被特殊处理。该单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。
这包括对如上所示换行符的支持,以及十六进制和 Unicode 代码等。转到链接部分以查看反斜杠转义字符的列表。
echo $'one\ntwo'
时,您会看到什么?
git commit -m "first line"$'\n'"second line"
。请注意,您必须在开始 $'string'
之前关闭前一个字符串。
没有必要使事情复杂化。在 -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 ...
[编辑 05-05-2021]
对于 Windows 用户,请使用 GitBash for Windows。内置 Windows cmd
不适用于此方法。
-m
因此,如果您正在寻找对多个提交消息进行分组,这应该可以完成工作:
git commit -m "commit message1" -m "commit message2"
我希望这不会离发布的问题太远,但是 setting the default editor 然后使用
git commit -e
可能会舒服得多。
-e
,它仍然会打开编辑器
做类似的事情
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"
在 Bash/Zsh 中,您可以简单地在引号内使用文字换行符:
git commit -m 'Multi-line
commit
message'
ANSI-C quoting 也适用于 Bash/Zsh:
git commit -m $'Multi-line\ncommit\nmessage'
您还可以指示 Git 使用您选择的编辑器来编辑提交消息。来自 git-commit 上的文档:
用于编辑提交日志消息的编辑器将从 GIT_EDITOR 环境变量、core.editor 配置变量、VISUAL 环境变量或 EDITOR 环境变量(按此顺序)中选择。有关详细信息,请参阅 git-var。
例如,要使用 nano
编辑您的消息,您可以运行:
export GIT_EDITOR=nano
git commit
我在 Mac 上使用 zsh,我可以在双引号 (") 内发布多行提交消息。基本上我一直在输入并按回车键换行,但在我关闭引号并返回之前,消息不会发送到 Git .
如果你使用 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'
以下是使用标准 cmd.exe shell 在 Windows 上失败的解决方案列表(为您节省一些试错时间!):
git commit -m 'Hello Enter 不起作用:它不会要求换行
git commit -m "你好输入同上
git commit -m "你好^输入同上
git commit -m 'Hello^ Enter World' 看起来像工作,因为它询问“更多?”并允许写一个新行,但最后在做 git log 你会看到它仍然是一个单行消息......
TL;DR:即使在 Windows 上,命令行解析工作 differently,并且 ^
允许多行输入,它在这里也无济于事。
最后,git commit -e
可能是最好的选择。
就个人而言,我发现在 vi
(或您选择的任何 git 编辑器)中而不是在命令行中修改提交消息最简单,方法是在 git commit
之后立即执行 git commit --amend
。
git commit -e
即可获得相同的结果而无需修改。
git commit
,它将打开一个带有提交消息模板的编辑器。
我没有看到有人提到如果您不提供消息,它将为您打开 nano(至少在 Linux 中),您可以在其中编写多行...
只需要这个:
git commit
可悲的是,git 似乎不允许在其消息中使用任何换行符。上面已经有各种合理的解决方案,但是在编写脚本时,这些很烦人。这里的文档也可以工作,但处理起来也可能有点烦人(想想 yaml 文件)
这是我所做的:
git commit \
--message "Subject" \
--message "First line$(echo)Second line$(echo)Third Line"
虽然这仍然很丑陋,但它允许“单行”可能仍然有用。由于通常字符串是变量或与变量组合,因此可以将丑陋保持在最低限度。
我一直在寻找该问题的任何答案,因为我曾经多次看到 regexes 来检查 commit 消息。无论如何,在使用 -m
标志创建提交期间没有任何想法。
https://i.stack.imgur.com/m2JZU.png
不需要更复杂的方法。
IMO 初始提交消息行应该是简短的,而不是段落。所以使用 git commit -m "<short_message>"
就足够了
之后,为了扩展初始提交消息,我们可以使用
git commit --amend
这将打开 vim,然后我们可以输入提交消息的解释,在我看来这比命令行更容易。
不定期副业成功案例分享
git
命令可以从标准输入读取消息文本,而-F
选项提供用于读取消息的文件名。