git add'* .txt'和git add * .txt有什么区别? [英] What's the difference between git add '*.txt' and git add *.txt?

查看:167
本文介绍了git add'* .txt'和git add * .txt有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在处理git课程 https://try.github.io/levels/1/challenges/1 ,并被git add'* .txt'和git add * .txt的不同行为所困惑。

没有引用的字符串不会添加所有内容。 git rm也一样。它为什么这样工作?或者它只是web版本特定的?

正如通配符在引号中,通配符在单引号或双引号中不起作用。


Fileglobs(例如 *。c )可用于添加所有匹配的文件





您可以在 t / t4010-diff-pathspec.sh#L53-L56






Commit 8300016 通过 Junio C Hamano( gitster


gitcli 增加了另一种防止文件通配的方法, :给予shell和git的对比通配符



不习惯使用shell的人可能智力上理解命令行参数如何被按摩但仍然难以想象让shell扩展fileglobs和让Git将fileglob用作pathspec的区别。



许多命令都允许通配符在路径,但是你需要保护
免受shell的影响。这两个意思是不同的
的东西:



  -------- ------------------------ 
$ git checkout - * .c
$ git checkout - \ * .c
--------------------------------





  • 前者让你的shell扩展fileglob,而你问
    点C您的工作树中的文件将被索引中的
    版本覆盖。

  • 后者将 *。c 传递给Git,并且您要求
    索引中的路径与模式被检出到你的
    工作树。



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

这篇关于git add'* .txt'和git add * .txt有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆