2015-12-21 93 views
3

不清楚git ls-files如何使用通配符实际上是功能。Git ls-files行为

我开始使用git ls-files *.*命令,它工作正常。它显示所有底层子目录下版本控制下的所有文件。 但现在我想选择一组文件。例如:"Device_*.xml"

所以我执行git ls-files Device_*.xml但这没有结果?!

我知道该命令是区分大小写的。哪里不对?以下是我用输出执行的命令列表。使用Git版本:2.6.1.windows.1

D:\GIT\repo>git clean -xdf 
D:\GIT\repo>git reset --hard 

HEAD现在是7de8f5b [IP-826-通用配置管理票]合并远程追踪分支 '原产地' 进入IP-826-通用构型管理票

D:\GIT\repo>git status 

分支IP-826-通用配置管理票

你的分支上最新与“产地/ IP-826-仿制配置 - 管理票”。

Untracked files: 
    (use "git add <file>..." to include in what will be committed) 

没有加入到承诺,但未跟踪文件存在(使用“混帐添加”追踪)

D:\GIT\repo\Imagepipe\SettingsDB\GeneratedDevicesAllPlatforms>dir Device_*.xml /s 

<LOT OF DEVICE_*.xml FILES HERE> 

12/10/2015 10:46    681 Device_GeneratedDevices_0-0-0_0.xml 
1 File(s)   681 bytes 

Directory of   D:\GIT\repo\Tools\DevTools\SettingsGenerator\SLIB2_GenerateSettings\Test\DB7\GeneratedDevices\D1 
12/10/2015 10:46    1,997 Device_D1_0-0-0_0.xml 
1 File(s)   1,997 bytes 

Directory of  D:\GIT\repo\Tools\DevTools\SettingsGenerator\SLIB2_GenerateSettings\Test\DB7\S_NOCHECK 

12/10/2015 10:46    1,558 Device_S_NOCHECK_0-0-0_0.xml 
1 File(s)   1,558 bytes 

Directory of  D:\GIT\repo\Tools\DevTools\SettingsGenerator\SLIB2_GenerateSettings\Test\DB7\S_TEST 

12/10/2015 10:46    1,536 Device_S_TEST_0-0-0_0.xml 
1 File(s)   1,536 bytes 

Total Files Listed: 
     968 File(s)  14,032,982 bytes 
      0 Dir(s) 18,400,256,000 bytes free 

D:\GIT\repo>git ls-files Device_*.xml 
D:\GIT\repo> 

**No result!** 
+0

有趣的是:“混帐LS-文件FOO - * .TXT” https://github.com/git/git/commit/de8bada2bf63d274b8f7759f0ffd0b7669e52eca – VonC

回答

3

的问题是,如果你在一个非字符串使用星号(*)环境下,命令行解释器将执行扩展本身:它将查找目录中的文件,在版本控制下不需要,并将它们添加为参数。例如,假设该目录包含一个文件:

foo-a.txt 
foo-b.txt 

和你打电话git ls-files foo-*.txt,其实你调用命令git ls-files foo-a.txt foo-b.txt。现在有可能foo-c.txt处于版本控制之下,但曾经被删除,并且该foo-a.txtfoo-b.txt不是,导致没有列出任何文件。

不过,若你使用星号字符串中的环境,像git ls-files "foo-*.txt"星号是由git解释。因此,如果存储库中存在与通配符匹配的文件,它将被返回。

> git init 
Initialized empty Git repository in /foo/bar/.git/ 
> ls 
> touch foo-c.txt 
> git add .; git commit -am 'foo-c' 
[master (root-commit) 3523fc3] foo-c 
1 file changed, 1 insertion(+) 
create mode 100644 foo-c.txt 
> rm foo-c.txt 
> git ls-files 
foo-c.txt 
> git ls-files foo-*.txt 
fish: No matches for wildcard 'foo-*.txt'. 
git ls-files foo-*.txt 
      ^
> git ls-files 'foo-*.txt' 
foo-c.txt 
> touch foo-a.txt 
> touch foo-b.txt 
> git ls-files 'foo-*.txt' 
foo-c.txt 
> git ls-files foo-*.txt 

在这个例子中,我们首先设置一个git仓库,那么我们创建一个文件foo-c.txt。现在我们添加该文件并进行提交。接下来我们删除文件。如果我们现在叫git ls-files foo-*.txt,它是fish(shell),它抱怨没有找到这样的文件。但是,如果我们在一个字符串中通过foo-*.txt,则gitfoo-c.txt匹配没有问题。

如果我们在以后添加foo-a.txtfoo-b.txt的目录,由字符串环境之外执行的通配符,git得到git ls-files foo-a.txt foo-b.txt,但因为没有这样的文件正在subversioning,它没有返回值(它无法找到这些文件)。如果你使用带字符串的通配符,它​​会再次出现foo-c.txt

+0

我试图重现此窗口下,但第一个命令仍返回FOO-C。 txt –

+0

@MarkdeBont:它应该返回'foo-c.txt',因为它是版本控制下的唯一文件。看例子。 –