Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I commit an empty folder in git?

Tags:

git

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:

DAG with tree nodes

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.

like image 784
Daniel Kaplan Avatar asked Sep 27 '14 22:09

Daniel Kaplan


People also ask

Can I commit an empty folder in Git?

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.

Why does Git ignore empty folders?

It ignores all directories. In Git, directories exist only implicitly, through their contents. Empty directories have no contents, therefore they don't exist.

Can you commit an empty file?

Yes, indeed, by design, you can not commit empty directories, containing no files, to a Git repository.

How do I add an empty directory 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.


2 Answers

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.

like image 182
fstanis Avatar answered Sep 18 '22 08:09

fstanis


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

like image 28
Piotr Oktaba Avatar answered Sep 17 '22 08:09

Piotr Oktaba