简体   繁体   中英

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

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?

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

Fileglobs (eg *.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM