0

I have the following structure:

public/

  • uploads/
    • cover-photos/
      • thumbnail/
    • profile-photos/
      • original/
      • thumbnail/

I would like to push those directories without any content in the server, so I tried to accomplish that by adding an empty .gitkeep file in each of those directories and used the following code:

/public/uploads/cover-photos/thumbnail/*
!/public/uploads/cover-photos/thumbnail/.gitkeep
/public/uploads/profile-photos/original/*
!/public/uploads/profile-photos/original/.gitkeep
/public/uploads/profile-photos/thumbnail/*
!/public/uploads/profile-photos/thumbnail/.gitkeep

This works fine only for the child directories, thumbnail and original. If I add any file there, git will ignore those, but if I add a file in (for example) /public/uploads/cover-photos/ then git will track it and push it. How can i achieve what I want for the rest of the directories?

zeeks
  • 775
  • 2
  • 12
  • 30
  • 1
    My preference is to use the `.gitignore` file as the reason that the directory gets created. That is, each "empty" directory contains one `.gitignore` file whose contents start with `*` and `!/.gitignore`. Add any sub-directories as `!/subdir/` to force them to be included as well, and start them off with the standard keep-this-directory `.gitignore` file with the two entries. – torek May 22 '22 at 04:24
  • 1
    This particular pattern is clear and obvious and never requires any additional thought, and yet is also easy to update should certain files need to be un-ignored (so only minor additional thought is required at that point). – torek May 22 '22 at 04:25

1 Answers1

0

You may do it with:

/public/uploads/*/**/*

!/public/uploads/cover-photos/thumbnail/
!/public/uploads/cover-photos/thumbnail/.gitkeep

!/public/uploads/profile-photos/original/
!/public/uploads/profile-photos/original/.gitkeep

!/public/uploads/profile-photos/thumbnail/
!/public/uploads/profile-photos/thumbnail/.gitkeep

The first line matches both /public/uploads/cover-photos/* and /public/uploads/cover-photos/thumbnail/* but not /public/uploads/* (you may use /public/uploads/**/* if you want files there included). However, it also ignores the sub-directories including the .gitkeeps (documentation):

It is not possible to re-include a file if a parent directory of that file is excluded.

So you will need to first re-include the directory and then the .gitkeeps.

!/public/uploads/profile-photos/thumbnail/
!/public/uploads/profile-photos/thumbnail/.gitkeep
Kana
  • 186
  • 1
  • 2
  • 3
  • That didn't work. If I add a file in the directory /public/uploads/cover-photos/ for example, git will detect the file there when I do not want it to. – zeeks May 22 '22 at 02:10
  • @zeeks I've set up an example project [here](https://github.com/gudzpoz/gitignore-example) and it works fine for me. Would you please check [if the file has already been tracked](https://stackoverflow.com/questions/1274057/) or give a more detailed example? – Kana May 22 '22 at 03:15