git .gitignore 排除目录中的文件但不排除某些目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5600621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.gitignore exclude files in directory but not certain directories
提问by Matthew
application/cache/*
application/cache/folder/*
application/cache/folder/onemorefolder/*
This doesn't seem to be working. When I clone the project, there is no "application/cache"
folder or "application/cache/folder"
folder, etc...
这似乎不起作用。当我克隆项目时,没有"application/cache"
文件夹或"application/cache/folder"
文件夹等...
I'd like if files in the cache folders weren't cached but folders were, so that the folders permissions transfer and exist.
我想如果缓存文件夹中的文件没有被缓存但文件夹被缓存,那么文件夹权限就会转移并存在。
回答by mipadi
Git doesn't track folders, only files, so if you ignore everything in a folder, Git won't have anything to track. You can add a .gitignore
file to each directory (application/cache
, application/cache/folder
, application/cache/folder/onemorefolder/
) with the following contents:
Git 不跟踪文件夹,只跟踪文件,因此如果您忽略文件夹中的所有内容,Git 将无法跟踪任何内容。您可以.gitignore
向每个目录 ( application/cache
, application/cache/folder
, application/cache/folder/onemorefolder/
)添加一个文件,其内容如下:
*
!.gitignore
Then, you can add those directories, and only the .gitignore
file in each directory will get added -- but this means the directories will now be tracked (i.e., created when cloning).
然后,您可以添加这些目录,并且只会.gitignore
添加每个目录中的文件——但这意味着现在将跟踪目录(即,在克隆时创建)。
回答by KingCrunch
Git doesn't track empty directories. Just add some empty placeholder files in the folders you want to be committed.
Git 不跟踪空目录。只需在要提交的文件夹中添加一些空的占位符文件即可。
touch application/cache/.keep
git add -f application/cache/.keep
Do this also with each "empty" folders. Later you can ignore these files, they really only exists to make sure that git creates those directories on clone. The entries in .gitignore
keeps others files within the folders from being tracked (unless you force it with git add -f
;)).
对每个“空”文件夹也执行此操作。稍后您可以忽略这些文件,它们的存在实际上只是为了确保 git 在克隆时创建这些目录。中的条目.gitignore
使文件夹中的其他文件不被跟踪(除非您用git add -f
;)强制它)。
回答by Joel Duckworth
There is another perhaps cleaner way to do this. Rather than having sub .gitignore files in the folders you want to keep. You can put this in the root .gitignore as follows:
还有另一种可能更清洁的方法来做到这一点。而不是在要保留的文件夹中包含子 .gitignore 文件。您可以将其放在根 .gitignore 中,如下所示:
application/cache/*
application/cache/folder/*
application/cache/folder/onemorefolder/*
!*.gitkeep
Now just create and commit empty .gitkeep files into the directories as listed above. The folder will then be tracked with those .gitkeep files but none of the contents will be tracked.
现在只需创建空 .gitkeep 文件并将其提交到上面列出的目录中。然后将使用这些 .gitkeep 文件跟踪该文件夹,但不会跟踪任何内容。
回答by Claude Janz
you can put a .gitignore file in each of it (like mipadi said) or make something like that on your root .gitingnore file
你可以在每个文件中放一个 .gitignore 文件(就像 mipadi 说的)或者在你的根 .gitingnore 文件上做类似的事情
/assets/*/
/assets/*.*
it works fine for me
这对我来说可以
回答by Stephen
Visual Studio didn't like the accepted answer. I had to add a new line before the * to make it work.
Visual Studio 不喜欢接受的答案。我必须在 * 之前添加一个新行才能使其工作。
# Ignore all files in this folder.
*
!.gitignore