WARNING: This tweak is not truly working as it turns out. Sorry for the inconvenience.
Original post below:
I found a solution while playing with Git internals!
Create your empty directory:
$ mkdir path/to/empty-folder
Add it to the index using a plumbing command and the empty tree SHA-1:
$ git update-index --index-info
040000 tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 path/to/empty-folder
Type the command and then enter the second line. Press Enter and then Ctrl + D to terminate your input. Note: the format is mode [SPACE] type [SPACE] SHA-1hash [TAB] path (the tab is important, the answer formatting does not preserve it).
That's it! Your empty folder is in your index. All you have to do is commit.
This solution is short and apparently works fine (see the EDIT!), but it is not that easy to remember...
The empty tree SHA-1 can be found by creating a new empty Git repository, cd into it and issue git write-tree, which outputs the empty tree SHA-1.
EDIT:
I've been using this solution since I found it. It appears to work exactly the same way as creating a submodule, except that no module is defined anywhere.
This leads to errors when issuing git submodule init|update.
The problem is that git update-index rewrites the 040000 tree part into 160000 commit.
Moreover, any file placed under that path won't ever be noticed by Git, as it thinks they belong to some other repository. This is nasty as it can easily be overlooked!
However, if you don't already (and won't)
Maybe adding an empty directory seems like it would be the path of least resistance because you have scripts that expect that directory to exist (maybe because it is a target for generated binaries). Another approach would be to modify your scripts to create the directory as needed.
mkdir --parents .generated/bin ## create a folder for storing generated binaries
mv myprogram1 myprogram2 .generated/bin ## populate the directory as needed
In this example, you might check in a (broken) symbolic link to the directory so that you can access it without the ".generated" prefix (but this is optional).
ln -sf .generated/bin bin
git add bin
When you want to clean up your source tree you can just:
rm -rf .generated ## this should be in a "clean" script or in a makefile
If you take the oft-suggested approach of checking in an almost-empty folder, you have the minor complexity of deleting the contents without also deleting the ".gitignore" file.
You can ignore all of your generated files by adding the following to your root .gitignore:
.generated
You can save this code as create_readme.php and run the PHP code from the root directory of your Git project.
php create_readme.php
It will add README files to all directories that are empty so those directories would be then added to the index.
<?php
$path = realpath('.');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
if ( is_dir($name) && ! is_empty_folder($name) ){
echo "$name\n" ;
exec("touch ".$name."/"."README");
}
}
function is_empty_folder($folder) {
$files = opendir($folder);
while ($file = readdir($files)) {
if ($file != '.' && $file != '..')
return true; // Not empty
}
}
?>
Then do
git commit -m "message"
git push