Key word here is why. There are plenty of questions where the answer is, "git doesn't allow that" but I want to know why it doesn't allow this.
I've been reading about the architecture of git and it has this image in it:
This image shows that there are tree
nodes. Technically, it looks like it'd be straight forward to save a tree
without any children. So why does git forbid this?
There's a part in this book that mentions this:
For each directory above the changed file (plus the repository root directory), a new tree object is created with a new identifier. A DAG is created starting from the newly created root tree object pointing to blobs (reusing existing blob references where the files content has not changed in this commit) and referencing the newly created blob in place of that file's previous blob object in the previous tree hierarchy. (A blob represents a file stored in the repository.)
I feel like this might be the reason, but it's kind of glossing over the details with respect to what I want answered.
git can't push empty directories. It can only track files. If you try to push a folder with nothing in it, although it will exist on your local machine, nothing will go into your branch.
It ignores all directories. In Git, directories exist only implicitly, through their contents. Empty directories have no contents, therefore they don't exist.
Yes, indeed, by design, you can not commit empty directories, containing no files, to a Git repository.
Second Solution The . gitignore file tells Git to add this file on the repository and add the folder ignoring all other files of that folder. The . gitignore file can be updated to allow additional files to be added to the folder whenever you need them.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With