Skip to content

Committing Empty Directories in Git: A Simple Command Line Solution

2 min read

If you're setting up a new Git repository and need to make sure that any empty directories get committed use this handy little command from the command line:-

for D in `find . -type d -empty`; do touch $D/empty; done

This will search for any empty directories and put an empty file in them that you can then add to Git to ensure the directory gets committed.

If you've already created a Git repository you'll want to exclude the .git directory when checking for empty directories. In which case use the following instead:-

for D in `find . -type d -empty -not -path "./.git/*"`; 
    do touch $D/empty; 
done

The reason for creating these empty files (which is what touch $D/empty is doing) is that Git won't let you commit actual directories, just the files inside them. So if the directory is empty it won't get committed. This can be a pain if you've got, for example, a cache directory that is required every time the repository is cloned, but you don't want to commit the cache files inside it. Just remember that you need to add the empty file to the the repository if you've got an ignore rule on the directory itself.

© 2025 Andy Carter