bash 通过命令行在 Git 提交消息中使用撇号(单引号)

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

Use of apostrophe (single-quote) in a Git commit message via command line

gitbashescaping

提问by nutty about natty

I am trying to take thisone step further. How could this work in a standard Bash shell?

我试图采取这种进一步的一步。这如何在标准 Bash shell 中工作?

git commit -m 'cracked enigma's code'

Could this simply be done with backslash-escaping like the following?

这是否可以简单地通过反斜杠转义来完成,如下所示?

git commit -m 'cracked enigma\'s code'

Further, how could double-quotes be used? Also by backslash-escaping? Would that be the best way? Are there any good alternative ways?

此外,如何使用双引号?还通过反斜杠转义?那会是最好的方法吗?有什么好的替代方法吗?

git commit -m 'cracked the "real" enigma's code'

回答by choroba

Use double quotes:

使用双引号:

git commit -m "cracked enigma's code"

Or, if your message contains other special characters, use double quotes or backslash only for the single quote:

或者,如果您的消息包含其他特殊字符,请仅对单引号使用双引号或反斜杠:

git commit -m 'cracked $enigma'"'"'s code'
git commit -m 'cracked $enigma'\''s code'

回答by Pigueiras

There is no need to escape the 'character if your commit is double quoted.

'如果您的提交是双引号,则无需转义该字符。

git commit -m "cracked enigma's code"

EDIT: Anyway, when you have some special characters to add in the commit message I prefer to edit in a editor (like nano or vim), commiting without the -moption.

编辑:无论如何,当您在提交消息中添加一些特殊字符时,我更喜欢在编辑器(如 nano 或 vim)中进行编辑,不带-m选项进行提交。

git commit

And then put the message and exit. It's more confortable instead of thinking how you have to escape all those quotes and double quotes.

然后把消息和退出。它更舒适,而不是考虑如何逃避所有这些引号和双引号。