Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git add -A :/ on Git 2.X and relationship with pathspec

Tags:

git

I have read in several places that the behavior of git add -A has changed a bit over time.

As of 2.x (e.g. Git 2.5.0), what does git add -A :/ exactly do? I couldn't find the option : or :/ in the documentation. Is it a pathspec? How so? The examples the documentation provides only show glob patterns (e.g. *.c) or simple path specifications (e.g. dir to add anything under dir).

like image 317
Josh Avatar asked Aug 11 '15 20:08

Josh


2 Answers

Since git 2.0, git add -A and git add -A :/ are the same.
But the magic pathspec :/ is not new and dates back from git 1.7.6 (Apr 2011). See commit 8a42c98. It is documented in Documentation/glossary-content.txt

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.

:top: or :/

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


Note that if you want to add a folder named ':/' (git add -A :/), this will be possible only in git 2.7 (Nov 2015)
See commit 29abb33 (25 Oct 2015) by Junio C Hamano (gitster).

Since Git 2.0, "add -u" and "add -A" run from a subdirectory without any pathspec mean "everything in the working tree" (before 2.0, they were limited to the current directory).
The limiting to the current directory was implemented by inserting "." to the command line when the end user did not give us any pathspec.
At 2.0, we updated the code to insert ":/" (instead of '.') to consider everything from the top-level, by using a pathspec magic "top".

(This is no longer needed, and fixed in said commit 29abb33: the implementation of git add -A no longer use :/ for the upcoming git 2.7)

Incidentally such a simplification also fixes a corner case bug that stems from the fact that ":/" does not necessarily mean any magic.
A user would say "git --literal-pathspecs add -u :/" from the command line when she has a directory ':' and wants to add everything in it (and she knows that her :/ will be taken as 'everything under the sun' magic pathspec unless she disables the magic with --literal-pathspecs).

The internal use of ':/' would behave the same way as such an explicitly given ":/" when run with "--literal-pathspecs", and will not add everything under the sun as the code originally intended.

Since that internal use of :/ is no more, a git --literal-pathspecs add -u :/ will actually work, and add files under the folder named "column" (':').

like image 129
VonC Avatar answered Nov 04 '22 07:11

VonC


git add -A :/ will add all changes to index even if you are currently not in the top level project folder. But in later git versions, it's the same as git add -A

Say you have your project like this:

➜  top git:(master) tree .
.
├── b.c
└── subfolder
    └── a.c

Then you changed code in b.c, but you are currently in subfolder

➜  subfolder git:(master) ✗ git st
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   ../b.c

no changes added to commit (use "git add" and/or "git commit -a")

Running git add . now will NOT add b.c to your index, and old git add -A will NOT too. But in older git, git add -A :/ will do the trick.

If no pathspec is given when -A option is used, all files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).

For more information, please read git help add git help gitglossary

like image 35
shengy Avatar answered Nov 04 '22 09:11

shengy