[英]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.