如何使用主题行和消息正文进行 git commit?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40505643/
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-19 12:25:05  来源:igfitidea点击:

How to do a git commit with a subject line and message body?

gitcommit

提问by weaveoftheride

I want to improve the way I do git commits and I've been reading around the web. I followed the site here http://chris.beams.io/posts/git-commit/and this lead me to https://git-scm.com/book/en/v2/Customizing-Git-Git-Configurationwhere I can set my default editor, but I still don't understand how I edit the subject line separately from the body of the commit.

我想改进我做 git commits 的方式,我一直在网上阅读。我关注了这里的网站http://chris.beams.io/posts/git-commit/这导致我到https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration其中我可以设置我的默认编辑器,但我仍然不明白我是如何将主题行与提交正文分开编辑的。

I'm used to doing:

我习惯于这样做:

git commit -am "message here"

but as I understand it for longer commits, I should use an editor like vim (on my mac)

但据我了解,对于更长的提交,我应该使用像 vim 这样的编辑器(在我的 mac 上)

回答by axiac

The easiest way to do it is to run git commitwithout -mand the message.

最简单的方法是在git commit没有-m消息的情况下运行。

(git commit -ais a shortcut for git add .; git commit)

(git commit -a是 的快捷方式git add .; git commit)

It will start an editor where you can enter the commit message on multiple lines. Then you save the file and when the editor exits, git commitcontinues and uses the message from the file.

它将启动一个编辑器,您可以在其中输入多行提交消息。然后保存文件,当编辑器退出时,git commit继续并使用文件中的消息。

If you prefer to do everything from a single command (or you write a script that needs to call git commitand this interactive way of committing is not an option) then you can provide the commit subject and the commit message body by using the -margument two times:

如果您更喜欢从单个命令执行所有操作(或者您编写了一个需要调用的脚本git commit并且这种交互式提交方式不是一种选择),那么您可以通过使用-m参数两次来提供提交主题和提交消息正文:

git commit -m "this is the subject" -m "this is the body"

Using -mmultiple times in the command line concatenates the messages as separate paragraphs (separated by an empty line). This works perfectly to provide the subject as the argument of the first -mand the message body as the argument of the second -m.

-m在命令行中多次使用将消息连接为单独的段落(由空行分隔)。这完美地提供了主题作为第一个的参数-m和消息正文作为第二个的参数-m

There is no easy way to embed newlines in the commit message body. Using three or more times the -moption will produce a commit message that contains empty lines and this is probably not what you want.

没有简单的方法可以在提交消息正文中嵌入换行符。使用该-m选项三次或更多次将产生一个包含空行的提交消息,这可能不是您想要的。

If you are on Linuxor macOSand you shell of choice is bashthen there is an ugly but working way to write a message body that contains new lines. Embed each line in quotes (") to allow them contain spaces and join the lines with $'\n'which is the bashway of writing special characters in the command line (or in a script).

如果您在LinuxmacOS您选择的外壳是bash那么有一种丑陋但有效的方法来编写包含新行的消息正文。将每一行嵌入引号 ( ") 中以允许它们包含空格并将行连接起来$'\n',这是bash在命令行(或脚本)中编写特殊字符的方式。

The command looks like this:

该命令如下所示:

git commit -m "the subject" -m "the first line"$'\n'"the second line"$'\n'"the third line"

回答by rodelarode

Run: git commit -aand you should get a VI editor interface that lets you enter Subject line and message. After finishing entering text press ESC then :wq

运行:git commit -a你应该得到一个 VI 编辑器界面,可以让你输入主题行和消息。输入完文本后按 ESC 然后 :wq