我正在从命令行使用Git,并试图在提交消息中添加换行符(使用Git commit -m "")而不进入Vim。
这可能吗?
我正在从命令行使用Git,并试图在提交消息中添加换行符(使用Git commit -m "")而不进入Vim。
这可能吗?
当前回答
在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创建的一个示例。
这适用于所有终端和操作系统AFAIK。
下面是bash的示例:
导致这个提交:
其他回答
在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提交中添加换行符
尝试以下命令创建多行提交消息:
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创建的一个示例。
这适用于所有终端和操作系统AFAIK。
下面是bash的示例:
导致这个提交:
就我个人而言,我发现在vi(或任何你选择的git编辑器)中修改提交消息比在命令行中更容易,在git提交后立即执行git commit——modify。
我一直在寻找这个问题的答案,因为我曾经多次看到用于检查提交消息的正则表达式。无论如何,在使用-m标志创建提交时,没有任何想法可以工作。
因此,实现多行提交消息的最简单方法是在终端中只输入git commit。 然后,你将有机会输入你想要的笔记:
不需要更复杂的方法了。
遗憾的是,git似乎不允许在其消息中出现任何换行符。上面已经有各种合理的解决方案,但在编写脚本时,这些都很烦人。这里文档也可以工作,但是处理起来可能有点麻烦(比如yaml文件)
以下是我所做的:
git commit \
--message "Subject" \
--message "First line$(echo)Second line$(echo)Third Line"
虽然这仍然很难看,但它允许“一行程序”,这可能仍然有用。由于字符串通常是变量或与变量组合在一起,因此可以将丑陋的字符串保持到最小。