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

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

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

我习惯了:

git commit -am "message here"

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

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

最简单的方法是 运行 git commit 不带 -m 和消息。

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

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

如果您更喜欢通过单个命令完成所有操作(或者您编写了一个需要调用 git commit 的脚本,并且这种交互式提交方式不是一种选择),那么您可以提供提交主题和按 using the -m argument two times:

提交消息正文
git commit -m "this is the subject" -m "this is the body"

在命令行中多次使用 -m 将消息连接为单独的段落(由空行分隔)。这非常适合将主题作为第一个 -m 的参数提供,将消息正文作为第二个 -m.

的参数提供

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

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

命令如下所示:

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