Why can't I commit an empty folder in git?
The reason for this is actually simple - directories in the git index only exist as part of the file paths.
If you have directories dir1
and dir2
, with dir1
containing fileA
and fileB
and dir2
containing fileC
, git will commit the following (i.e. add the following to the index file):
dir1/fileA
dir1/fileB
dir2/fileC
In other words, neither dir1
nor dir2
are truly committed, they are simply constructed implicitly based on the paths of files contained inside them.
Now whether there are any deeper reasons for this or it simply made the implementation easier, I don't know.
GIT FAQ is pretty straightforward about it:
Currently the design of the Git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.
Link:
https://git.wiki.kernel.org/index.php/GitFaq#Can_I_add_empty_directories.3F
It's actually entirely possible to create and commit and external directory using the low-level commands git mktree
and git commit-tree
(git ls-tree
is useful for inspection). I think this is useful for mirroring other VCS's that do support empty directories. It's only the high-level commands that don't support it.