0

I`ve the following directory structure and i want to add the directories "public" and "brand" to git, but no other subdirectories of /app/:

/app
--.gitignore
--/public
----.gitkeep
----/brand
------.gitkeep

The /app/.gitignore file looks as follows:

*
!public/.gitkeep
!public/brand/.gitkeep
!.gitignore

However, only the "public" directory is added to git but not the .gitkeep files nor the directory "brand". Do you have an idea what´s wrong with the .gitignore file?

Mike
  • 152
  • 1
  • 9
  • 1
    If you don't include the `.gitkeep` files how will git know to keep the directory in the repo? Either way: you will have to add and commit the `.gitignore` to your repo first. Have you done that? – Peter Krebs Jul 21 '22 at 13:53
  • AFAIK, Git doesn't store folders, instead it stores files and their paths. – evolutionxbox Jul 21 '22 at 13:58
  • 1
    Due to the `*`, your `/public` directory gets ignored, and once a directory is ignored, it's impossible to include any of its files even with the `!` statements. Quote from https://git-scm.com/docs/gitignore#_pattern_format : «It is not possible to re-include a file if a parent directory of that file is excluded.» – qrsngky Jul 21 '22 at 14:01
  • I personally dislike the `.gitkeep` file trick. We know (from the fact that Git stores only files and submodules, not folders; see [here](https://stackoverflow.com/q/115983/1256452) for details, including a submodule trick you can use if you want) that we want to put a file into the directory just for Git purposes. We also know that we want Git to exclude all *other* files in this directory. So why not put in a `.gitignore` file that has two lines, `*` and `!/.gitignore`? It's simple and explicit: we put *exactly this file* in every directory we want to store as an empty directory. – torek Jul 21 '22 at 16:51

1 Answers1

0

Thanks for your inputs! Here´s the solution that works for me now:

*
!public/
!public/**/
!public/**/.gitkeep
Mike
  • 152
  • 1
  • 9