bash 使用通配符的“git add”没有像我希望的那样运行 - 我必须进入特定目录吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12066184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"git add" using wildcard is not functioning as I hoped - must I cd into specific directories?
提问by RudyOnRails
When I try to do a basic git add *.erb
(or any simple wildcard expressions) git is not recognizing it (them). As a side-note, I have never done this before so I'm sure it's a rookie mistake, but I've found no help in other SO posts or my school's alumni listserv so I thought this post may be appropriate.
当我尝试做一个基本的git add *.erb
(或任何简单的通配符表达式)时,git 无法识别它(它们)。作为旁注,我以前从未这样做过,所以我确定这是一个新手错误,但我在其他 SO 帖子或我学校的校友列表服务中没有找到任何帮助,所以我认为这篇文章可能是合适的。
For (a different) example, git statusis giving me:
对于(不同的)示例,git status给了我:
# modified: config/routes.rb
# modified: spec/models/question_spec.rb
I wanted to only stage the routes file, so I tried git add *s.rband no dice. I am in the root of the app...do I need to be in the directory that contains the file(s) I'm trying to apply the wildcard expression to? That would be painful, but...actually...it just worked.
我只想暂存路由文件,所以我尝试了git add *s.rb而没有骰子。我在应用程序的根目录中...我是否需要在包含我尝试应用通配符表达式的文件的目录中?那会很痛苦,但是......实际上......它只是有效。
Hope this doesn't have to be a separate post, but is there an easier way to use wildcards where you don't have to cd into the specific directory?
希望这不必是一个单独的帖子,但是有没有更简单的方法来使用通配符,而不必 cd 进入特定目录?
回答by user673207
Put it around single quotes. This should work.
把它放在单引号周围。这应该有效。
git add '*s.rb'
git add '*s.rb'
UPDATE
更新
After more testing, I find out that I can even do git add *s.rb
without quotes. This might just work for me with my git version 1.7.10.4 (Mac OSX Lion, homebrew). My explanation is that if shell wildcards expansion doesn't match any file, it'd supply the original unexpanded argument '*s.rb' to git add
.
经过更多测试,我发现我什至可以git add *s.rb
不用引号。这可能只适用于我的 git 版本 1.7.10.4(Mac OSX Lion,自制软件)。我的解释是,如果 shell 通配符扩展与任何文件都不匹配,它会将原始未扩展的参数 '*s.rb' 提供给git add
.
sh-3.2$ git status
#
# modified: config/routes.rb
# modified: spec/models/products_spec.rb
#
Now I do git add
without quotes.
现在我git add
没有引号。
sh-3.2$ git add *s.rb
Surprisingly it works!
令人惊讶的是它有效!
sh-3.2$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: config/routes.rb
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: spec/models/products_spec.rb
#
If wildcard pattern doesn't match any file, git will give out this error.
如果通配符模式与任何文件都不匹配,git 将给出此错误。
sh-3.2$ git add *x.rb
fatal: pathspec '*x.rb' did not match any files
回答by Michael Anderson
I can't get recursive globs to work for me as my version of bash is too old. So what I do is
由于我的 bash 版本太旧,我无法让递归全局变量对我来说有效。所以我要做的是
find . -name "*.rb" | xargs git add
Works a treat and I can add other filters there if I need - so if I want to add all but one file I can do this.
很好用,如果需要,我可以在那里添加其他过滤器 - 所以如果我想添加除一个文件之外的所有文件,我可以这样做。
find . -name "*.rb" | grep -v "not_me.rb" | xargs git add
There's a couple of other cases that can be useful.
还有一些其他情况可能有用。
If you just want to add files that already exist in your repo you can
如果您只想添加存储库中已经存在的文件,您可以
git add -u .
If you want to add every thing then
如果您想添加所有内容,那么
git add .
回答by Antimony
In my experience (I may be missing an option or something), git add only looks at files in the current directory and subdirectory. So if you want to add everything, go to the root directory of the repo. Though if the repo is really big, this could take a while, especially if you don't pass the -u flag.
根据我的经验(我可能缺少一个选项或其他东西), git add 只查看当前目录和子目录中的文件。所以如果要添加所有内容,请转到 repo 的根目录。虽然如果 repo 真的很大,这可能需要一段时间,特别是如果你没有通过 -u 标志。