bash 从命令行向“git commit -m”添加换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5064563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:12:49  来源:igfitidea点击:

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

gitbashshell

提问by Alan Whitelaw

I am using Git from the command line and am trying to add a line break to the commit message (using git commit -m "") without going into Vim.

我正在从命令行使用 Git,并试图在git commit -m ""不进入 Vim 的情况下向提交消息(使用)添加换行符。

Is this possible?

这可能吗?

采纳答案by Simon Richter

Certainly, how it's done depends on your shell. In Bash, you can use single quotes around the message and can just leave the quote open, which will make Bash prompt for another line, until you close the quote. Like this:

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

git commit -m 'Message

goes
here'

Alternatively, you can use a "here document" (also known as heredoc):

或者,您可以使用“此处文档”(也称为 heredoc):

git commit -F- <<EOF
Message

goes
here
EOF

回答by Simon

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."

Note that this creates separate paragraphs - not lines. So there will be a blank line between each two -mlines, e.g.:

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

My head line

My content line.

回答by esse

Using Git from the command line with Bash you can do the following:

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

git commit -m "this is
> a line
> with new lines
> maybe"

Simply type and press Enterwhen you want a new line, the ">" symbol means that you have pressed Enter, and there is a new line. Other answers work also.

只需键入并Enter在需要新行时按,“>”符号表示您已按Enter,并且有一个新行。其他答案也有效。

回答by Paused until further notice.

You should be able to use

你应该可以使用

git commit -m $'first line\nsecond line'

From the Bash manual:

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.

$' string'形式的词被特殊处理。该单词扩展为 string,并按照 ANSI C 标准的规定替换反斜杠转义字符。

This includes support for newlines as shown above, plus hex and Unicode codes and others. Go to the linked section to see a list of the backslash-escaped characters.

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

回答by Jon Crowell

Adding line breaks to your Git commit

在 Git 提交中添加换行符

Try the following to create a multi-line commit message:

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

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."

Then verify what you've done:

然后验证你做了什么:

git log -1

You should end up with something like this:

你应该得到这样的结果:

Multi-line Git commit message in PowerShell

PowerShell 中的多行 Git 提交消息

The screenshot is from an example I set up using PowerShell with Poshgit.

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

回答by Peter Farmer

Doing something like

做类似的事情

git commit -m"test\ntest"

doesn't work, but something like

不起作用,但类似

git commit -m"$(echo -e "test\ntest")"

works, but it's not very pretty. You set up a git-commitlbcommand in your PATHwhich does something like this:

作品,但它不是很漂亮。你git-commitlb在你的命令中设置了一个命令,PATH它执行如下操作:

#!/bin/bash

message=

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

And use it like this:

并像这样使用它:

git commitlb "line1\nline2\nline3"

Word of warning, I have a feeling that the general convention is to have a summary line as the first line, and then two line breaks, and then an extended message in the commit message, so doing something like this would break that convention. You could of course do:

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

git commitlb "line1\n\nline2\nline3"

回答by Saravanan M

From Git documentation:

Git 文档

-m <msg>
--message=<msg>
Use the given <msg> as the commit message. If multiple -moptions are given, their values are concatenated as separate paragraphs.

-m <msg>
--message=<msg>
使用给定的 <msg> 作为提交消息。如果-m给出多个选项,它们的值将连接为单独的段落。

So, if you are looking for grouping multiple commit messages this should do the work:

因此,如果您正在寻找对多个提交消息进行分组,这应该可以完成工作:

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

回答by Tobse

I hope this isn't leading too far away from the posted question, but setting the default editorand then using

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

git commit -e

might be much more comfortable.

可能会舒服很多。

回答by blongho

There is no need complicating the stuff. After the -m "text...the next line is gotten by pressing Enter. When Enteris pressed >appears. When you are done, just put "and press Enter:

没有必要使事情复杂化。-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 ...

回答by Abizern

I use zsh on a Mac, and I can post multi-line commit messages within double quotes ("). Basically I keep typing and pressing return for new lines, but the message isn't sent to Git until I close the quotes and return.

我在 Mac 上使用 zsh,我可以在双引号 (") 内发布多行提交消息。基本上我一直输入并按回车键换行,但是直到我关闭引号并返回后,消息才会发送到 Git .