简体   繁体   English

Git,设置.gitignore 忽略所有文件EXCEPT.conf 文件,也包括子目录

[英]Git, set up .gitignore to ignore all files EXCEPT .conf files, including subdirectories too

How to get Git to ignore all files in a directory and ALSO in its subdirectories except for.conf files?如何让Git忽略目录中的所有文件以及除 .conf 文件之外的子目录中的所有文件?

Currently I have this in my.gitignore:目前我在 my.gitignore 中有这个:

!/some/dir/
/some/dir/*
!/some/dir/**/
!/some/dir/**/*.conf

So, I want to ignore all files in /some/dir/ and also its subdirectories, except for *.conf files.所以,我想忽略 /some/dir/ 及其子目录中的所有文件,*.conf 文件除外。

But it is not ignoring any file in /some/dir/.但它不会忽略 /some/dir/ 中的任何文件。

For example, /some/dir/somefile is not ignore, /some/dir/subdir/anotherfile is not ignore also.例如,/some/dir/somefile 不是忽略,/some/dir/subdir/anotherfile 也不是忽略。 I just want to not ignore files like: /some/dir/subdir/file.conf.我只想不忽略以下文件:/some/dir/subdir/file.conf。

You may wish to tweak this in various ways, but here's the simplest thing I know of that meets your stated requirements.您可能希望以各种方式对此进行调整,但这是我所知道的最简单的方法,可以满足您的要求。 Create a .gitignore file inside some/dir that reads:some/dir中创建一个.gitignore文件,内容为:

*
!*/
!*.conf

and you're all set.一切就绪。

Here is an example:这是一个例子:

$ mkdir test-ignore
$ cd test-ignore
$ git init
Initialized empty Git repository in ...
$ echo test ignore rules > README
$ git add README 
$ git commit -m initial
[master (root-commit) 33962fc] initial
 1 file changed, 1 insertion(+)
 create mode 100644 README
$ mkdir -p some/dir/a some/dir/b
$ touch some/dir/file some/dir/a/a.conf some/dir/b/b.conf some/dir/a/ignored
$ cat > some/dir/.gitignore << END
> *
> !*/
> !*.conf
> END
$ git add .
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   some/dir/a/a.conf
        new file:   some/dir/b/b.conf

$ 

Explanation解释

  • * means ignore everything (that is currently untracked, of course; tracked files cannot be ignored). *表示忽略所有内容(当然,当前未跟踪;跟踪的文件不能被忽略)。
  • */ means if it's a directory, don't ignore it, do search it , and being later in the file, this overrides a previous entry; */表示如果它是一个目录,不要忽略它,搜索它,并且在文件的后面,这会覆盖以前的条目; of course, being searched does not make files in the directory get added, but the subdirectory does get scanned;当然,被搜索并不会添加目录中的文件,但会扫描子目录;
  • .* conf means if a file's base name matches *.conf , don't ignore it , so any files in some/dir named that are named *.conf , or any files in any subdirectory of some/dir that are named *.conf , are not-ignored and hence get added. .* conf表示如果文件的基本名称与*.conf匹配,则不要忽略它,因此some/dir中名为*.conf的任何文件,或some/dir的任何子目录中名为 * 的任何文件*.conf ,不会被忽略,因此会被添加。

That's why some/dir/a/a.conf did get added, but some/dir/a/ignored did not.这就是为什么some/dir/a/a.conf确实被添加了,但some/dir/a/ignored没有。

(You didn't specify in text what you want to do with some/dir/somefile and some/dir/thing.conf so I had the same rules apply here: file is ignored and thing.conf is not-ignored. I think that's what you intended with .**/* conf .) (你没有在文本中指定你想对some/dir/somefilesome/dir/thing.conf做什么,所以我在这里应用了相同的规则: file被忽略, thing.conf未被忽略。我认为这就是你想要的.**/* conf 。)

In general, moving a .gitignore file "down the tree" as far as it goes will simplify it, so it's easier to put this in some/dir/.gitignore .通常,将.gitignore文件“沿树”尽可能地移动会简化它,因此将其放在some/dir/.gitignore中会更容易。 The general rule */ , close enough to the end of a .gitignore , forces Git not to short-circuit the scanning of subdirectories found recursively.一般规则*/足够接近.gitignore的末尾,强制 Git 不要短路递归找到的子目录的扫描。 Other than these two rules (which should be obvious, but aren't always obvious at first), the rest is obvious.除了这两个规则(应该很明显,但起初并不总是很明显)之外,rest 很明显。

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

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