8

I have seen colleagues using git add -A :/ for staging files in repositories, but I am unable to find what that does in the documentation. What am I missing?

Note: I understand what the flag -A does (this question has been answered in SO before). My question is specifically about :/ and the role it plays in git add.

Community
  • 1
  • 1
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • "git add -A" is equivalent to "git add .; git add -u" "git add -A" is just a short cut of doing both – Moonhead Feb 26 '14 at 16:51
  • 1
    I *think* `:` it refers to the root directory of the repository, but I haven't found that in the documentation. – Keith Thompson Feb 26 '14 at 16:57
  • 1
    I've never seen that syntax before, but it looks similar to that of [refspec](http://git-scm.com/book/ch9-5.html). – Ryne Everett Feb 26 '14 at 17:06
  • 1
    @KeithThompson Have a look at the definition of path spec in https://www.kernel.org/pub/software/scm/git/docs/gitglossary.html – lrineau Feb 26 '14 at 18:24

2 Answers2

8

As you already know the -A option, let's talk about git add :/ only. According to the documentation of git-add, the last argument is a pathspec. The definition of it is in the documentation of gitglossary. Let me quote the releant parts (I put the important sentences in bold):

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 :), and the remainder is the pattern to match against the path. The optional colon that terminates the "magic signature" can be omitted if the pattern begins with a character that cannot be a "magic signature" and is not a colon.

In the long form, the leading colon : is followed by a 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.

The "magic signature" consists of an ASCII symbol that is not alphanumeric.

top /

The magic word top (mnemonic: /) makes the pattern match from the root of the working tree, even when you are running the command from inside a subdirectory.

Currently only the slash / is recognized as the "magic signature", but it is envisioned that we will support more types of magic in later versions of git.

You can see that if a pathspec begins by :/ or :(top) then that part of the pathspec is by definition the root of the working tree.

git add :/ stages all files in the working tree.

Community
  • 1
  • 1
lrineau
  • 6,036
  • 3
  • 34
  • 47
  • The documentation of `git-add` (at least the one you linked to) does not seem to mention **pathspec** anywhere. I just fixed it for you. – Amelio Vazquez-Reina Feb 26 '14 at 18:45
  • @user815423426 I do not understand. The SYNOPSIS of [git-add(1)](http://git-scm.com/docs/git-add) and the first paragraph of OPTIONS both mention *pathspec*. But the definition of a *pathspec* is hard to find. It is described in [gitglossary(7)](https://www.kernel.org/pub/software/scm/git/docs/gitglossary.html) – lrineau Feb 27 '14 at 09:35
  • @user815423426 Sorry. I read you edit, and indeed the link to `git-add(1)` I gave was a different version. On my machine, I have Git-1.8.3 and its documentation pages, and it does talk about *pathspec*. Older versions talk about filepattern. – lrineau Feb 27 '14 at 09:38
4

(This answer originally talked about refspecs, which turned out to be irrelevant and incorrect.)

As lrineau's answer correctly points out, the : character in this case is part of the syntax of a pathspec.

Documentation on pathspecs is annoyingly difficult to find, but there's a "gitglossary" man page, available either by typing man gitglossary or visiting this web page.

The relevant part:

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 :), and the remainder is the pattern to match against the path. The optional colon that terminates the "magic signature" can be omitted if the pattern begins with a character that cannot be a "magic signature" and is not a colon.

In the long form ... [snip].

The "magic signature" consists of an ASCII symbol that is not alphanumeric.

top /
The magic word top (mnemonic: /) makes the pattern match from the root of the working tree, even when you are running the command from inside a subdirectory.

The conclusion is the same as in my original answer: :/ refers to the root directory of the current working tree.

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • But `git add` takes path specifiers, not refspecs. The `:/` syntax smells of after-the-fact workaround for old `git add` behavior: see http://stackoverflow.com/a/21954767/1256452 – torek Feb 26 '14 at 18:12
  • 1
    That is a **pathspec** and not a **refspec**. – lrineau Feb 26 '14 at 18:16
  • @lrineau: Please feel free to edit my answer or post your own. I use git, but I've managed to avoid learning about refspecs and pathspecs. – Keith Thompson Feb 26 '14 at 19:06
  • I have improved my answer by quoting gitglossary(7), as you did. – lrineau Feb 27 '14 at 09:50