2

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. Only if followed by a branch name.
Can you please give me some references to learn more about this?

1 Answer 1

3

It's actually a bit trickier than you might expect, as :/A could be three things.

There are multiple different bits of Git documentation that talk about this, but there are two main places to look:

When it's just a file name, :/A just means the file named :/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.

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):

... This name returns the youngest matching commit which is reachable from any ref, including 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.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean with "no terminating :" ? I didn't understand what does ' : ' do in a pathspec
The pathspec section says A pathspec that begins with a colon : has special meaning. In the short form, the leading colon : is followed by zero or more "magic signature" letters (which optionally is terminated by another colon :) ... So :/: has the optional terminating colon.
It goes on to say: In the long form, the leading colon : is followed by an open parenthesis (, a comma-separated list of zero or more "magic words", and a close parentheses ), and the remainder is the pattern to match against the path. I get tripped-up by this, writing, e.g., :(top,icase):path/name before remembering that there's no optional terminating colon, so that this tries to match /:path/name ignoring case, rather than matching path/name ignoring case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.