git - 什么时候应该在 git 中使用引号?

标签 git quotes

为了举例,假设我有

Directory/
      nameOfFile.txt
      nameOfDirectory/
                ...

我一直使用格式 git add Directory/nameOf* 但在不同的文档中,我看到 git add 'Directory/nameOf*'

刚刚尝试了 git add "Directory/nameOf*" 并且成功了。

还尝试了 git commit -m 不带引号的消息,它也有效。

那么 Git 是否允许无引号/引号互换,或者这只是某些情况或版本?除了它允许的范围之外,无引号、单引号和双引号的标准协议(protocol)是什么?

最佳答案

这个问题实际上与 git 无关,而与你的 shell 有关。

大多数 shell 使用空格“标记”命令行——即将其分割成一系列离散元素。因此,例如...

rm one file

...将尝试删除名为 one 的文件和名为 file 的文件,而...

rm 'one file'

...将尝试删除名为one file的单个文件。因此,对于您的示例,是否使用引号并不特别重要,因为您的文件名都不包含空格。一个异常(exception)是 commit 示例;如果 message 包含空格,您需要引用它,否则您将得到:

$ git ci -m this is a test
error: pathspec 'is' did not match any file(s) known to git.
error: pathspec 'a' did not match any file(s) known to git.
error: pathspec 'test' did not match any file(s) known to git.

事实上,还有一点值得考虑:引用文本通常会抑制通配符扩展,因此如果我有一个名为 nameOfFile.txt 的文件并且我这样做...

rm nameOf*.txt

...它会工作得很好,但如果我这样做:

rm 'nameOf*.txt'

...我会收到错误:

rm: cannot remove `nameOf*.txt': No such file or directory

但是,如果 shell 不这样做,git 实际上会自行执行文件名扩展,因此带有通配符的示例将起作用。

关于git - 什么时候应该在 git 中使用引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819608/

相关文章:

java - 如何在 Java 中用空格替换双引号?

regex - R 正则表达式 : isolate a string between quotes

r - 如何在 R 数据框中查找和替换双引号

bash - sed,引号中的正斜杠

git - 我应该怎么做才能从上次推送的提交中获取源代码?

android - 构建 Android 电子邮件应用程序

git - 如何为 Git 存储库组织和设置镜像备份服务器?

string - Julia 中的单引号和双引号

git - 可以在 github 中启用 word-diff 选项以查看对一行的更精细更改吗?

git - `git push --mirror` 的对面?我如何取回我的 repo 协议(protocol)?