简体   繁体   English

Git忽略目录和目录/*有什么区别?

[英]What's the difference between Git ignoring directory and directory/*?

I'm confused about what's the correct way to ignore the contents of a directory in git.我对忽略 git 中目录内容的正确方法感到困惑。

Assume I have the following directory structure:假设我有以下目录结构:

my_project  
     |--www  
         |--1.txt  
         |--2.txt
     |--.gitignore

What's the difference between putting this:放这个有什么区别:

www

And this?和这个?

www/*

The reason I'm asking this question is: In git, if a directory is empty, git won't include such empty directory in repository.我问这个问题的原因是:在 git 中,如果一个目录是空的,git 不会在存储库中包含这样的空目录。 So I was trying the solution that is add an extra .gitkeep file under the directory so that it won't be empty.所以我正在尝试在目录下添加一个额外的 .gitkeep 文件的解决方案,这样它就不会为空。 When I was trying that solution, if in the .gitignore file, I write like below:当我尝试该解决方案时,如果在 .gitignore 文件中,我会这样写:

www
!*.gitkeep

It doesn't work(My intention is to ignore all contents under www but keep the directory).它不起作用(我的意图是忽略 www 下的所有内容,但保留目录)。 But if I try the following:但是,如果我尝试以下操作:

www/* 
!*.gitkeep

Then it works!然后它起作用了! So I think it must has some differences between the two approaches.所以我认为这两种方法之间肯定存在一些差异。

There're differences among www , www/ and www/* . wwwwww/www/*之间存在差异。

Basically from the documentation and my own tests, www find a match with a file or a directory, www/ only matches a directory, while www/* matches directories and files inside www .基本上从文档和我自己的测试, www找到一个文件或目录,匹配www/只匹配一个目录,而www/*匹配的目录和文件的内部www

I'll only discuss on the differences between www/ and www/* here, since the differences between www and www/ are obvious.这里只讨论www/www/*的区别, wwwwww/的区别很明显。

For www/ , git ignores the directory www itself, which means git won't even look inside.对于www/ ,git 会忽略目录www本身,这意味着 git 甚至不会查看内部。 But for www/* , git checks all files/folders inside www , and ignores all of them with the pattern * .但对于www/* ,混帐检查所有文件/文件夹内的www ,而忽略他们都与模式* It seems to lead to the same results since git won't track an empty folder www if all its child files/folders are ignored.这似乎会导致相同的结果,因为如果忽略所有子文件/文件夹,git 将不会跟踪空文件夹www And indeed the results will be no difference for OP's case with www/ or www/* standalone.事实上,对于 OP 与www/www/*独立的情况,结果没有区别。 But it does make differences if it's combined with other rules.但如果与其他规则结合起来,它确实会有所不同。

For example, what if we want to only include www/1.txt but ignore all others inside www ?例如,如果我们只想包含www/1.txt而忽略www所有其他内容怎么办?

The following .gitignore won't work.以下.gitignore将不起作用。

www/
!www/1.txt

While the following .gitignore works, why?虽然以下.gitignore有效,为什么?

www/*
!www/1.txt

For the former, git just ignores the directory www , and won't even look inside to include www/1.txt again.对于前者,git 只是忽略目录www ,甚至不会再查看内部以包含www/1.txt The first rule excludes the parent directory www but not www/1.txt , and as a result www/1.txt cannot be " included again ".第一条规则排除父目录www但不排除www/1.txt ,因此www/1.txt不能“再次包含”。

But for the latter, git first ignores all files/folers under www , and then includes one of them again which is www/1.txt .但对于后者,git 首先忽略www下的所有文件/文件夹,然后再次包含其中一个www/1.txt

For this example, the follwing lines in the documentation may help:对于此示例,文档中的以下几行可能会有所帮助:

An optional prefix "!"一个可选的前缀“!” which negates the pattern;这否定了模式; any matching file excluded by a previous pattern will become included again.任何被先前模式排除的匹配文件将再次包含在内。 It is not possible to re-include a file if a parent directory of that file is excluded.如果排除了该文件的父目录,则无法重新包含该文件。

I'm just parsing through the documentation, and as far as I can tell they only differ in more advanced patterns, eg我只是在解析文档,据我所知,它们仅在更高级的模式中有所不同,例如

$ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar

I did test the above, and if you replace !/foo with !/foo/* , you do indeed get a different result.确实测试了上述内容,如果您将!/foo替换为!/foo/* ,您确实会得到不同的结果。

Note笔记

foo

Will exclude any file foo , but将排除任何文件foo ,但

foo/

will only exclude directories named foo.只会排除名为 foo 的目录。

Apart from the perfectly good answers you have already obtained, you should note that you can have .gitignore anywhere in your project, including subfolders.除了您已经获得的完美答案之外,您还应该注意,您可以在项目的任何位置使用.gitignore ,包括子文件夹。

So if you want to ignore all files inside www , but whant the www folder to be versioned, instead of using an empty .gitkeep , .dummy or whatever name you choose, why not use a .gitignore there, telling to ignore all files?因此,如果您想忽略www所有文件,但希望对www文件夹进行版本控制,而不是使用空的.gitkeep.dummy或您选择的任何名称,为什么不在那里使用.gitignore ,告诉忽略所有文件?

/
|- .gitignore   (a)
\- www
    |- .gitignore   (b)
    |- 1.jpg
    \- 2.jpg

In the root .gitignore (a), you don't say anything about the www folder or its contents.在根.gitignore (a) 中,您没有.gitignore www文件夹或其内容。

In the www/.gitignore (b) you put the following:www/.gitignore (b) 中,您输入以下内容:

# ignore all files in this folder except this .gitignore
*
!.gitignore

This way everything looks more organized (to me at least).这样一切看起来更有条理(至少对我来说)。

To ignore everything in a directory except dotfiles you can use the following glob-pattern in your .gitignore :要忽略目录中除 dotfiles 之外的所有内容,您可以在.gitignore使用以下全局模式:

www/[^.]*

So no need for an extra .gitignore , just simply add a .keep file to your www directory.所以不需要额外的.gitignore ,只需简单地将.keep文件添加到您的www目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM