Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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" (':').


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...