bash 通过命令行在 Git 提交消息中使用感叹号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5131948/
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
Use of an exclamation mark in a Git commit message via the command line
提问by Daniel X Moore
How do I enter an exclamation point into a Git commit message from the command line?
如何从命令行在 Git 提交消息中输入感叹号?
It is possible to escape the exclamation point with a backslash, but then the backslash ends up in the commit message as well.
可以用反斜杠转义感叹号,但反斜杠也会出现在提交消息中。
I want something like this:
我想要这样的东西:
git commit -am "Nailed it!"
回答by SiegeX
Use single quotes instead of double quotes
使用单引号代替双引号
git commit -am 'Nailed it!'
Alternatively, if you need to use double quotes for whatever reason but still want a literal !then turn off history expansion at the top of your script via set +H
或者,如果您出于某种原因需要使用双引号但仍需要文字,!则通过以下方式关闭脚本顶部的历史扩展set +H
回答by nicky_zs
Another way to solve that is to add a spaceafter !like:
要解决的另一个方法是添加一个空格后!喜欢:
git commit -am "Nailed it! "
Note the spacebetween !and the last ".
注意中间的空格!和最后一个“。
(The space won't be included in the commit message - Git trims trailing whitespace from commit messages automatically.)
(空格不会包含在提交消息中 - Git 会自动修剪提交消息中的尾随空格。)
回答by PDK
No need to remember what quotes or escapes to use, instead
无需记住要使用的引号或转义符,而是
- Type your command (or part of it)
- In your command line, press
CtrXE? will open your "external editor". - Edit your command, and close your editor.
- 输入您的命令(或其中的一部分)
- 在命令行中,按
CtrXE? 将打开您的“外部编辑器”。 - 编辑您的命令,然后关闭您的编辑器。
… the command will be executed & any special characters will show up correctly in the Git commit message!
...命令将被执行,任何特殊字符都将正确显示在 Git 提交消息中!
(NB many other uses: typing complex commands, adding line breaks in your commit message etc.)
(注意许多其他用途:键入复杂的命令,在提交消息中添加换行符等)
回答by Ryan H.
If you need to use double quotes and the !is the last character in the message, just keep the !outside of the quotes, since we're only using quotes so that the spaces get included in the message.
如果您需要使用双引号并且 the!是消息中的最后一个字符,请保留!引号的外部,因为我们仅使用引号以便将空格包含在消息中。
git commit -m "Reverting last commit because I don't like it"!
If you need to include !mid string, you can use single quotes but if you need to use a literal single quote, you'll need to close your quote, then put the 'outside of the string by escaping it. So, let's say your message is I don't like it! Reverting again!, this can be composed with 'I don'+ \'+ 't like it! Reverting again!'
如果需要包含!中间字符串,可以使用单引号,但如果需要使用文字单引号,则需要关闭引号,然后'通过转义将其放在字符串的外部。所以,假设你的消息是I don't like it! Reverting again!,这可以用'I don'+ \'+组成't like it! Reverting again!'
git commit -m 'I don'\''t like it! Reverting again!'
Anything more complicated than this, you're probably better off with git commitand letting git invoke your default text editor :)
任何比这更复杂的事情,你可能最好git commit让 git 调用你的默认文本编辑器:)
回答by cg22
You can also type:
您还可以键入:
git commit -am "Nailed it
!
"
So just hit enter and place the exclamation mark on a new line, then close off the commit message. Found that one out by accident one day.
因此,只需按 Enter 键并将感叹号放在新行上,然后关闭提交消息。有一天偶然发现了一个。
回答by Steve Geluso
Hmm. Escaping it with a slash doesn't show up in commit message for me.
唔。用斜杠转义它不会出现在我的提交消息中。
git commit -m "Nailed it\!"

