Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Git add *.txt VS Git add'*.txt'

+3 votes
I was just doing the git tutorial posted on moodle https://try.github.io/levels/1/challenges/7 and was confused by the difference between git add'*.txt' and git add *.txt.
asked Aug 27 in CSC 285 (F17) by Samih Sghier (660 points)

1 Answer

+2 votes
Best answer

"This is the same in the shell as in the other grammars that you mention. A single quoted string will be treated as a "string literal" (so to speak).

The difference between git add '*.txt' and git add *.txt is who's doing the matching of the pattern against filenames.

In the case of git add '*.txt', git is doing the pattern matching. Since the shell won't expand the string literal '*.txt', git add will be called with a single argument, *.txt.  git then does the matching against the filenames available in the whole repository (because... git).

In the case of git add *.txt, the shell does the filename matching and will pass a list of matching filenames from the current directory to git add. Note that if there are no names matching the given pattern, the shell will (usually1) pass the pattern on to git add unexpanded. If this happens, the result will be the same as if the pattern had been quoted.

1 Usually, but see e.g. the failglob shell option in bash. See also comments to this answer.

When git add gets a filename pattern, it will add not only the files matching in the current directory, but it will add all files that matches in the whole repository (i.e. including any subdirectories). This is why the text in the lower right hand corner says


We need quotes so that Git will receive the wildcard before our shell can interfere with it. Without quotes our shell will only execute the wildcard search within the current directory. Git will receive the list of files the shell found instead of the wildcard and it will not be able to add the files inside of the octofamily directory."

From user Kusalananda on stackExchange. https://unix.stackexchange.com/questions/388759/what-is-the-difference-between-txt-and-txt/388767#388767 

answered Aug 28 by Daniel Zwiener (3,504 points)
selected Aug 29 by Samih Sghier
I was wondering the same thing and stumbled upon a similar article to what you found!