1

I understand I can create .gitignore files in directories and then add the directories, and those files to the project. However:

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
jeffmcneill
  • 2,052
  • 1
  • 30
  • 26
  • 2
    "Is there a way of adding directories without having files in them" No. Git knows nothing of directories. A commit contains only files with their pathnames. – matt May 31 '21 at 04:46
  • 2
    As for your other question, does this answer it? [How can I stage and commit all files, including newly added files, using a single command?](https://stackoverflow.com/questions/2419249/how-can-i-stage-and-commit-all-files-including-newly-added-files-using-a-singl) – matt May 31 '21 at 04:51
  • There is one trick that works to store an empty directory: use [the empty-repository submodule](https://stackoverflow.com/a/58543445/1256452). (I don't really advise using it; a `.gitignore` or `.gitkeep` empty file is usually a better idea.) – torek May 31 '21 at 10:43
  • @torek that looks like it changes directories into submodules, which seems a bit overkill in terms of complexity, but thank you for pointing out this option. – jeffmcneill Jun 01 '21 at 14:13
  • *seems a bit overkill* That's one reason I don't really recommend it. Submodules in general are painful. If Git's index could store directory entries, Git could store empty directories in commits; that would be better. – torek Jun 01 '21 at 19:51

1 Answers1

0

As stated Git track directories only if it contains at least one file, so a well accepted pratice is to add an hidden file called .gitkeep.

About the second question, the answer is NO, you have to add the .gitkeep in order to track the directory.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • This answer was helpful as it has a script that creates empty files if the directories are empty: https://unix.stackexchange.com/questions/463044/recursively-create-empty-file-in-empty-sub-directories – jeffmcneill May 31 '21 at 06:42