情景
我正在尝试从git存储库的整个历史中删除一些文件。他们都有几个共同的标准:
下面是文件树的一个例子:
root-directory/
|-> apples/
| |-> bad-settings-alpha.txt
| |-> bad-settings-beta.txt
|
|-> oranges/
| |-> bad-settings-gamma.txt
| |-> bad-settings-delta.txt
| |-> navels/
| |-> good-settings.txt
|
|-> good-settings.txt我需要过滤掉所有的bad-settings文件,同时保留good-settings文件。
我的方法
因此,通过使用GitHub提供的教程和git-rm手册,我构建了这个命令(分为两行):
git filter-branch -f --index-filter 'git rm --dry-run --cached \
--ignore-unmatch root-directory/*/*settings*.txt' --prune-empty -- --all这里要特别注意的是我使用的文件glob:root-directory/*/*settings*.txt。如果我在ls中使用该文件glob,那么我就会得到我想要删除的文件列表。所以,应该管用的,对吧?
显然不是。如果我用那个glob运行我的命令,它也会将所有的设置文件删除到比2更深的级别上。在上面的文件树示例中,这意味着root-directory/oranges/navels/good-settings.php将被核弹化。
我尝试自己解决这个问题,尝试对文件glob进行变体,并使用用于git-rm的出色的git-rm选项。似乎什么都起不了作用--我所能做的就是改变文件树的深度,从那里开始删除设置文件。
我确实发现了一件似乎与我的问题极其相关的事情。在git-rm的手册中,有这样的例子:
git rm Documentation/\*.txt
Removes all *.txt files from the index that are under the Documentation
directory and any of its subdirectories.
Note that the asterisk * is quoted from the shell in this example; this
lets git, and not the shell, expand the pathnames of files and
subdirectories under the Documentation/ directory.“从位于all...files及其任何子目录下的索引中删除the...directory”与实际发生的情况一致。真正有趣的是提到被引用的星号。据我所知,这允许git-rm处理文件glob展开,而不是bash。好吧。但这就留下了以下问题:
bash应该做扩展。如果这是真的,而且我的文件glob适用于ls,那么为什么它不适用于git-rm我也看到了上面的例子,它似乎做了我想要做的事情。然而,这不会发生在我身上,否则我就不会在这里了。不过,它似乎确实证实了我想用bash进行文件扩展。
发布于 2012-06-05 18:46:29
为什么不使用find显示两个级别的深度文件:
find . -maxdepth 2 -mindepth 2 -type f -name "bad-settings*"这将给你的坏设置的名单,只有两个层次深的指导。您可以通过xargs将它们输送到xargs:
find . -maxdepth 2 -mindepth 2 -type f -name "bad-settings*" | xargs git rmhttps://stackoverflow.com/questions/10902662
复制相似问题