简体   繁体   English

git add'* .txt'和git add * .txt有什么区别?

[英]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. 我只是在代码学校处理git课程https://try.github.io/levels/1/challenges/1 ,并被git add'* .txt'和git add * .txt的不同行为搞糊涂了。

String without quoted did not add everything. 没有引用的字符串没有添加所有内容。 Same for git rm. 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. 正如“ 引号内部的通配符”中所提到的,globbing在单引号或双引号中都不起作用。

Fileglobs (eg *.c ) can be given to add all matching files 可以给出Fileglobs(例如*.c )来添加所有匹配的文件

  • Without quotes, globbing would be done by the shell in the current folder. 没有引号,globbing将由当前文件夹中的shell完成。
  • 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). 使用引号,它可以防止shell的通配,并允许Git(与我的初始答案相反)处理所有工作树(而不仅仅是当前文件夹)中的pathspec*.txt ”。

You can see examples in t/t4010-diff-pathspec.sh#L53-L56 您可以在t/t4010-diff-pathspec.sh#L53-L56查看示例


Commit 8300016 by Junio C Hamano ( gitster ) adds another way to prevent file globbing, plus some explanation: Junio C 提交的8300016 Hamano gitster增加了另一种防止文件通配的方法,还有一些解释:

gitcli : contrast wildcard given to shell and to git gitcli :对比shell和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. 不习惯使用shell的人可能在理智上理解命令行参数是如何被shell按摩的,但是仍然很难想象让shell扩展fileglobs和让Git看到fileglob用作pathspec之间的区别。

Many commands allow wildcards in paths, but you need to protect them from getting globbed by the shell. 许多命令允许在路径中使用通配符,但是您需要保护它们不被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. 前者允许你的shell扩展fileglob,并且你要求工作树中的dot-C文件被索引中的版本覆盖。
  • 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. 后者将*.c传递给Git,并且您要求将索引中与模式匹配的路径检出到您的工作树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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