简体   繁体   English

如何在gitignore中编写规则以忽略除某些目录中的目录之外的所有文件?

[英]How can I write a rule in gitignore to ignore all files except directories in some directory?

So lets say I have a directory structure like so 所以我要说我有一个像这样的目录结构

uploads/
--dir1/
----one.txt
----two.txt
----dir2/
------one.txt
------two.txt

I would like any directory and sub directories of uploads to be tracked but not any files in it with the exception of a dummy file because git doesn't track directories. 我希望跟踪上传的任何目录和子目录,但不能跟踪其中的任何文件,除了虚拟文件,因为git不跟踪目录。

I would think something like this would work but it doesn't. 我认为这样的事情会奏效,但事实并非如此。

*
!.gitignore

When you tell it to ignore * , Git will not recurse into any subdirectories (even though you have a “unignore” pattern that might match something inside one of the directories). 当你告诉它忽略* ,Git不会递归到任何子目录中(即使你有一个“unignore”模式可能与其中一个目录内的东西相匹配)。

There is a bit of the gitignore pattern language that might help though. 有一些gitignore模式语言可能会有所帮助。 A trailing slash forces the pattern to only apply to directories. 尾部斜杠强制模式仅应用于目录。 So, you should be able to use this: 所以,你应该可以使用这个:

# .gitignore
*
!.keep
!*/

In the demonstration below I use the above .gitignore and have an additional file (named do-not-keep ) alongside each .keep file. 在下面的演示中,我使用上面的.gitignore并在每个.keep文件旁边有一个额外的文件(名为do-not-keep )。 You can see that it works for multiple levels of subdirectories and does not show the other files. 您可以看到它适用于多个子目录级别,并且不显示其他文件。 You will have to arrange for each directory to have its own .keep (or whatever) via some independent method. 您必须通过一些独立的方法安排每个目录拥有自己的.keep (或其他)。

% git status --short -uall
?? a/.keep
?? a/b/.keep
?? a/b/c/.keep
?? a/b/c/d/.keep
?? e/.keep
?? e/f/.keep
?? e/f/g/.keep
?? h/.keep
?? h/i/.keep
?? j/.keep

This was done with Git 1.7.3.1, but I expect that it will work for other versions as well. 这是通过Git 1.7.3.1完成的,但我希望它也适用于其他版本。

The demonstration setup: 演示设置:

% git init
% mkdir -p a/b/c/d e/f/g h/i j
% zargs -i.. -- **/*(/) -- touch ../.keep ../do-not-keep
% tree -aI .git .                                       
.
|-- .gitignore
|-- a
|   |-- .keep
|   |-- b
|   |   |-- .keep
|   |   |-- c
|   |   |   |-- .keep
|   |   |   |-- d
|   |   |   |   |-- .keep
|   |   |   |   `-- do-not-keep
|   |   |   `-- do-not-keep
|   |   `-- do-not-keep
|   `-- do-not-keep
|-- e
|   |-- .keep
|   |-- do-not-keep
|   `-- f
|       |-- .keep
|       |-- do-not-keep
|       `-- g
|           |-- .keep
|           `-- do-not-keep
|-- h
|   |-- .keep
|   |-- do-not-keep
|   `-- i
|       |-- .keep
|       `-- do-not-keep
`-- j
    |-- .keep
    `-- do-not-keep

10 directories, 21 files

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

相关问题 如何忽略除顶级目录中的几个文件以及.gitignore的几个目录中的所有内容之外的所有内容 - How do I ignore everything except a couple files in the top-level directory and everything in a few directories with .gitignore 我如何才能.gitignore目录中除以pattern开头的文件以外的所有文件? - How can I .gitignore all files in directory except those starting with pattern? 如何通过使用.gitignore忽略我需要的目录以外的所有文件和目录 - how to ignore all file and directory except a directory I need by using .gitignore 忽略根目录中的所有文件,但某些特定文件,目录除外 - Ignore all the files in the root except for some particular files, directories .gitignore无法忽略除文件夹以外的所有文件 - .gitignore fails to ignore all files except folders .gitignore无法忽略文件​​夹目录或文件 - .gitignore can't ignore folder directories or files 如何忽略.gitignore的wwwroot / js目录中的所有更改文件 - How to ignore all changes files in wwwroot/js directory in .gitignore .gitignore:如何忽略嵌套目录? - .gitignore: How do I ignore nested directories? git 如何忽略某个目录中的所有文件和子目录,但有一些例外? - How to git ignore all files and subdirectories from a certain directory except with some exceptions? .gitignore-忽略文件目录 - .gitignore - directory of ignore files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM