git:如何递归添加目录子树中与 glob 模式匹配的所有文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2221408/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:48:21  来源:igfitidea点击:

git: How do I recursively add all files in a directory subtree that match a glob pattern?

gitglobgit-add

提问by Ph??ng Nguy?n

I have several .screen files inside /xxx/documentationand its subdirectories that are already tracked by Git.

我在里面有几个 .screen 文件/xxx/documentation及其子目录,这些文件已经被 Git 跟踪了。

After modifying many of these screen files, I run git add documentation/\\*.screen—as indicated by the first example in git-add's documentation—to stage these files, but the command fails:

在修改了许多这些屏幕文件后,我运行git add documentation/\\*.screen- 如git-add's 文档中的第一个示例所示- 来暂存这些文件,但命令失败:

fatal: pathspec 'documentation/\*.screen' did not match any files

Is my command bad, or does git have a bug?

是我的命令不好,还是 git 有错误?

回答by Greg Bacon

It's a bug in the documentation. Quote the asterisk with

这是文档中的错误。引用星号

$ git add documentation/\*.screen

or

或者

$ git add 'documentation/*.screen'

to get the behavior you want.

得到你想要的行为。

If instead you want to add files in the current directory only, use

如果您只想在当前目录中添加文件,请使用

$ git add *.screen

UPDATE:I submitted a patchthat corrects the issue, now fixedas of version 1.6.6.2.

更新:我提交了一个补丁来纠正这个问题,现在从 1.6.6.2 版开始修复

回答by Flueras Bogdan

I've tried the accepted answer, but it didn't worked for me.. so here's mine just in case someone wants to get it's job done without spending time in dissecting various aspects that might cause the problem:

我已经尝试了公认的答案,但它对我没有用。

find documentation -name "*.screen" | xargs git add -u

//the -u option to git-add adds to index just the files that were previously tracked and modified

// git-add 的 -u 选项仅将先前跟踪和修改的文件添加到索引中

回答by Barnaby Dawson

This what I just used for a similar problem of git adding all the files in a directory:

这是我刚刚用于 git 添加目录中所有文件的类似问题的内容:

find . | sed "s/\(.*\)/\"\"/g" | xargs git add 

For the original question the command would be:

对于原始问题,命令将是:

find -name "*.screen" | sed "s/\(.*\)/\"\"/g" | xargs git add 

Note that I'm dealing with the case where a fully specified file name contains spaces. Thats why my answer. Edit the portion before the first |in order to pick out different files to add.

请注意,我正在处理完全指定的文件名包含空格的情况。这就是为什么我的回答。编辑第一个之前的部分|,以便挑选不同的文件进行添加。

回答by Aaron Digulla

You told the shell to look for *.screen(i.e. exactly this string - which doesn't exist - instead of what you want "all files that end with .screen). Omit the \\so the shell can do the file name expansion for you.

您告诉 shell 查找*.screen(即正是这个字符串——它不存在——而不是你想要的“所有以 结尾的文件.screen)。省略\\以便 shell 可以为你进行文件名扩展。

回答by Hymanie

git add *.java works for me to add recursively all the java files

git add *.java 对我来说递归地添加所有的java文件

回答by kubi

try

尝试

git add ./documentation/*.screen