Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between git add '*.txt' and git add *.txt?

Tags:

git

github

I was just processing git course https://try.github.io/levels/1/challenges/1 at code school and was confused by different behaviour of git add '*.txt' and git add *.txt.

String without quoted did not add everything. Same for git rm. Why does it work this way? Or it's just web version specific?

like image 424
avasin Avatar asked Nov 01 '15 12:11

avasin


People also ask

What is the difference between git add and git add *?

git add -A command will add all modified and untracked files in the entire repository. Whereas git add . will only add modified and untracked files in the current directory and any sub-directories. If you are at the root of the repo, they have the same effect.

What is the difference between git add and git commit?

Add and commit changesgit add : takes a modified file in your working directory and places the modified version in a staging area. git commit takes everything from the staging area and makes a permanent snapshot of the current state of your repository that is associated with a unique identifier.

What is git add file?

git add [filename] selects that file, and moves it to the staging area, marking it for inclusion in the next commit. You can select all files, a directory, specific files, or even specific parts of a file for staging and commit. This means if you git add a deleted file the deletion is staged for commit.

What is git add option?

The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .


1 Answers

As mentioned in "Wildcards inside quotes"", globbing doesn't work in either single- or double-quotes.

Fileglobs (e.g. *.c) can be given to add all matching files

  • Without quotes, globbing would be done by the shell in the current folder.
  • With quotes, it prevents globbing by the shell, and allows Git (contrary to my initial answer) to process the pathspec "*.txt" accross the all worktree (and not just the current folder).

You can see examples in t/t4010-diff-pathspec.sh#L53-L56


Commit 8300016 by Junio C Hamano (gitster) adds another way to prevent file globbing, plus some explanation:

gitcli: contrast wildcard given to shell and to git

People who are not used to working with shell may intellectually understand how the command line argument is massaged by the shell but still have a hard time visualizing the difference between letting the shell expand fileglobs and having Git see the fileglob to use as a pathspec.

Many commands allow wildcards in paths, but you need to protect them from getting globbed by the shell. These two mean different things:

--------------------------------
$ git checkout -- *.c
$ git checkout -- \*.c
-------------------------------- 
  • The former lets your shell expand the fileglob, and you are asking the dot-C files in your working tree to be overwritten with the version in the index.
  • The latter passes the *.c to Git, and you are asking the paths in the index that match the pattern to be checked out to your working tree.
like image 141
VonC Avatar answered Sep 28 '22 08:09

VonC