简体   繁体   中英

Git ignore subfolder

just trying to git ignore a subfolder in my project. It looks like so:

platforms/android/fileshere.txt

now I'd like to ignore all files in platforms/android, but I want to keep the folder structure.

Can't seem to find how to do that.

I tried: platforms/android/*

But it ignores the folders as well.

Thanks for any help

The basic problem is that git fundamentally always "ignores folders". git is designed to only track file content. If that content happens to be in a folder structure, that folder structure is stored with it -- but it is not possible for git to store directory structures with no files in it.

The common way around this problem that the git community at large has adapted is to add an empty file called ".gitkeep" inside each folder you want to keep . Then tell git to track that file. For example, when you create a new rails project, you will see there are ".gitkeep" files -- this demonstrates how people get around that problem.

If you explicitly tell git to track .gitkeep files, I do not think you need to make them an exception in your .gitignore file. Keep it as is, and I think that git will continue to ignore all files, but still keep track of the .gitkeep files, and therefore the directory structure as well. So your .gitignore (I think) could just say platforms/android/*

EDIT: This article is written by the git maintainer and addresses why that decision was made to store "content, not files". In fact, the first article he links to is by Linus himself (the creator of git) and is very helpful for some people to understand the ultimate reasoning behind what causes this very strange symptom of not being able to track folders directly.

The common practice is to maintain a .gitkeep file in each folder you would want to maintain. Therefore, your .gitignore file might look like:

platforms/android
!platforms/android/.gitkeep

You could just add a .gitignore file to folder you want to keep and for the contents of the file:

*

This way the folder will exist in clones but the contents of the folder will be ignored.

Yes - you can have multiple .gitignore files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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