.gitignore exclude files in directory but not certain directories

GitGitignore

Git Problem Overview


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

I'd like if files in the cache folders weren't cached but folders were, so that the folders permissions transfer and exist.

Git Solutions


Solution 1 - Git

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:

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

Solution 2 - Git

Git doesn't track empty directories. Just add some empty placeholder files in the folders you want to be committed.

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

Solution 3 - Git

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:

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.

Solution 4 - Git

you can put a .gitignore file in each of it (like mipadi said) or make something like that on your root .gitingnore file

/assets/*/
/assets/*.*

it works fine for me

Solution 5 - Git

Visual Studio didn't like the accepted answer. I had to add a new line before the * to make it work.

# Ignore all files in this folder.
*
!.gitignore

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthewView Question on Stackoverflow
Solution 1 - GitmipadiView Answer on Stackoverflow
Solution 2 - GitKingCrunchView Answer on Stackoverflow
Solution 3 - GitJoel DuckworthView Answer on Stackoverflow
Solution 4 - GitClaude JanzView Answer on Stackoverflow
Solution 5 - GitStephenView Answer on Stackoverflow