如何在gitignore中编写规则来忽略除某些目录中的目录以外的所有文件? [英] How can I write a rule in gitignore to ignore all files except directories in some directory?

查看:73
本文介绍了如何在gitignore中编写规则来忽略除某些目录中的目录以外的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想要跟踪上传的任何目录和子目录,但不是任何因为git不会跟踪目录,所以除了一个虚拟文件外,其他的文件都是这样。



我认为像这样的东西可以工作,但它不会。

 !.gitignore 


解决方案

当你让它忽略 * 时,Git不会递归到任何子目录中(即使你有一个unignore模式可能与里面的东西匹配其中一个目录)。



有一些 gitignore

一个>模式语言,但可能有帮助。尾部斜线强制该模式仅适用于目录。所以,你应该可以使用这个:

 #.gitignore 
*
!.keep
!* /

在下面的演示中,我使用上面的。 gitignore 并在每个 .keep 旁边有一个额外的文件(名为 do-not-keep ) >文件。您可以看到它适用于多级子目录,并且不显示其他文件。你必须通过一些独立的方法安排每个目录有自己的 .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

这是用Git 1.7.3.1完成的,但我期望它可以用于其他版本。



演示设置:

 %git init 
%mkdir -pa / b / c / de / f / gh / ij
%zargs -i .. - ** / *(/) - touch ../.keep .. /不要保留
%树-aI .git。

| - .gitignore
| - a
| | - .keep
| | - b
| | | - .keep
| | | - c
| | | | - .keep
| | | | - d
| | | | | - .keep
| | | | ` - 不要保留
| | | ` - 不要保留
| | ` - 不要保留
| ` - 不要保留
| - e
| | - .keep
| | - 不要保留
| ` - f
| | - .keep
| | - 不要保留
| ` - g
| | - .keep
| ` - 不要保留
| - h
| | - .keep
| | - 不要保留
| ` - 我
| | - .keep
| ` - 不要保留
` - j
| - .keep
` - 不要保留

10个目录,21个文件


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.

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).

There is a bit of the gitignore pattern language that might help though. 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. 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.

% 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.

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

这篇关于如何在gitignore中编写规则来忽略除某些目录中的目录以外的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆