简体   繁体   English

如何在我的 git 提交消息中提交代码块

[英]How do I commit code blocks in my git commit messages

My problem我的问题

While writing a commit message, I tried to use the " ` " mark to format the message with code.在编写提交消息时,我尝试使用“`”标记用代码格式化消息。
This is my commit message:这是我的提交信息:

git commit -am"style(Nav.tsx): adheres to eslint rules

line 92 and 122: add `@returns` in docstrings"

After I execute the command, I get this执行命令后,我得到了这个

bash: @returns: command not found
[auth 57ee20c] style(Nav.tsx): adheres to eslint rules
1 file changed, 27 insertions(+), 6 deletions(-)

And the commit message cuts off.并且提交消息被切断。 On GitHub, the line in question looks like this在 GitHub 上,有问题的行看起来像这样

line 92 and 122: add  in docstrings

What I have tried我试过的

I have tried using three of the " ` " thingies and I have also tried using <code></code>我尝试过使用三个“`”东西,我也尝试过使用<code></code>
When I use "```", I get the same message and problem, and when I use <code></code> , the command not found error is gone, but the commit message retains the <code></code> blocks instead of converting it.当我使用 "```" 时,我得到相同的消息和问题,当我使用<code></code>时, command not found错误消失了,但提交消息保留了<code></code>块而不是转换它。 I have also tried string escaping like this我也试过这样的字符串 escaping

line 92 and 122: add \`@returns\` in docstrings

And the like the code blocks, the commit message retains the " ` " instead of converting it.和代码块一样,提交消息保留“`”而不是转换它。

In Bash, double-quoted strings are subject to command substitution *.在 Bash 中,双引号字符串受命令替换*。 That's what the backticks are doing in the first case, and why you're seeing bash: @returns: command not found .这就是反引号在第一种情况下所做的,以及为什么您会看到bash: @returns: command not found To fix it, you can escape them, like you did:要修复它,您可以像以前一样逃避它们:

git commit -am "style(Nav.tsx): adheres to eslint rules

...
line 92 and 122: add \`@returns\` in docstrings"

or use single quotes:或使用单引号:

git commit -am 'style(Nav.tsx): adheres to eslint rules

...
line 92 and 122: add `@returns` in docstrings'

But, the backticks will be retained.但是,将保留反引号。 Commit messages are plain text.提交消息是纯文本。 The fancy formatting in that screenshot is being done by GitHub, not git.该屏幕截图中的精美格式由 GitHub 完成,而不是 git。 I believe GitHub supports Markdown in commit messages.我相信 GitHub 在提交消息中支持Markdown (Stack Exchange also uses Markdown.) (Stack Exchange 也使用 Markdown。)


* BTW if you did want to do command substitution in Bash, you should use the new syntax, $(command) instead of `command` . * 顺便说一句,如果您确实想在 Bash 中进行命令替换,您应该使用新语法$(command)而不是`command`

What probably happened大概发生了什么

Your shell (probably bash or some bourne shell variant) has a feature to execute commands and use the result to build a new command line.您的 shell(可能是 bash 或某些版本的 shell 变体)具有执行命令并使用结果构建新命令行的功能。 One of the ways to access that feature is by backticks.访问该功能的一种方法是通过反引号。 You can eg do你可以例如做

echo "Today is `date`."

to show the output of date within a nice little sentence.在一个漂亮的小句子中显示date的 output。

What you can do instead to have ` in your commit message您可以做些什么来在您的提交消息中包含`

  • Use single quotes ( ' ) around the message argument.在消息参数周围使用单引号 ( ' )。 Bash and sh don't perform command substitution in single-quoted strings. Bash 和 sh 不在单引号字符串中执行命令替换。

    or或者

  • Compose the commit message in an editor, rather than passing it on the command line.在编辑器中编写提交消息,而不是在命令行中传递它。 Do so by omitting the -m option and its argument.通过省略-m选项及其参数来做到这一点。 Git will then open the editor specified by the environment variable $EDITOR to let you edit the commit message. Git 将打开环境变量$EDITOR指定的编辑器,让您编辑提交消息。

Commit messages are plain text提交消息是纯文本

To Git and its command line tools (eg git log ), commit messages are plain text, without any markup language.对于 Git 及其命令行工具(例如git log ),提交消息是纯文本,没有任何标记语言。 Hosting platforms like GitHub and third-party tools may interpret that plain text as some markup language in some context, but not necessarily in all contexts. GitHub 等托管平台和第三方工具可能会在某些情况下将纯文本解释为某种标记语言,但不一定在所有情况下都如此。 Wherever GitHub does that, it'll usually be interpreting it as GitHub flavored MarkDown.无论 GitHub 在哪里这样做,它通常都会将其解释为 GitHub 风味的 MarkDown。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM