简体   繁体   English

git如何解释冒号和路径?

[英]How does git interpret a colon followed by a path?

I was toying with a syntax to refer a commit by its commit message and then I got this error: 我在玩弄一种通过提交消息引用提交的语法,然后出现此错误:

$ git show :/A
fatal: ambiguous argument ':/A': both revision and filename
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

The error message is pretty clear about how to solve it, but I haven't found any documentation explaining how does git interpret a colon followed by a path. 错误消息非常清楚如何解决,但是我还没有找到任何文档来解释git如何解释冒号和路径。 Only if followed by a branch name. 仅在后接分支名称时。
Can you please give me some references to learn more about this? 您能否给我一些参考以了解更多信息?

It's actually a bit trickier than you might expect, as :/A could be three things. 实际上,这比您预期的要复杂一些,因为:/A可能是件事。

There are multiple different bits of Git documentation that talk about this, but there are two main places to look: Git文档中有许多不同的地方对此进行讨论,但是有两个主要的地方需要考虑:

When it's just a file name, :/A just means the file named :/A . 当只是文件名时, :/A仅表示名为:/A的文件。

As a pathspec , :/A uses the "magic signature" character / , no terminating : , and the name A , so it refers to the file named A in the root directory (of the repository or work-tree), rather than the file :/A in the current directory. 作为路径规范:/A使用“魔术签名”字符/ ,不以:终止,并使用名称A ,因此它引用(存储库或工作树的) 根目录中名为A的文件,而不是当前目录中的文件:/A

As a revision specifier , :/A searches commit messages, and A becomes a regular expression (although in this case it's a trivial regex that just matches the letter A ): 作为修订说明符:/A搜索提交消息,并且A成为正则表达式(尽管在这种情况下,这是一个与字母A匹配的琐碎正则表达式):

... This name returns the youngest matching commit which is reachable from any ref, including HEAD. ...此名称返回可从任何引用(包括HEAD)访问的最年轻的匹配提交。

Using the -- you'll force Git to treat it as a pathspec , where you may want to add :(literal) in front to protect it from interpretation. 使用--您将强制Git将其视为pathspec ,您可能需要在其中添加:(literal)以防止其解释。

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

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