0

The answer to "How can I add an empty directory to a Git repository?" is you can't through regular git commands, because the staging only has files, not folders.

Commits, on the other hand, do have folders, so a commit could conceivably have an empty folder.

How would create a commit with an empty folder? What about a commit to remove an empty folder? Can I add the empty folder on top of other changes (made via the staging area)?

Note: Here is evidence that commits can contain folders:

$ git cat-file -p 6c07af3
tree 3d6e2028b57fba5cd2c5f1f01cdd5be9814335ec
author *** 1452398070 -0500
committer *** 1452398070 -0500

A commit

$ git cat-file -p 3d6e20
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    file1
040000 tree e4af7700f8c091d18cc15f39c184490125fb0d17    folder

Note that it stores which things are files and which are folders, as well as file and folder names. What I would want to do is create my own git commit object, without going through the staging process.

Community
  • 1
  • 1
PyRulez
  • 10,513
  • 10
  • 42
  • 87
  • Be aware: The asker is downvote any attempt to answer – CodeWizard Jan 10 '16 at 03:52
  • 3
    That question contains an answer http://stackoverflow.com/a/8944077/1256452 that is as close as you can get: you can use the semi-secret empty tree as an entry in a regular tree. Note all the caveats in the answer, though; the short version is "don't do it". – torek Jan 10 '16 at 03:58
  • @codeWizard Although I am glad at the interest in this question, all the answers so far are wrong, and the voting reflects this. – PyRulez Jan 10 '16 at 04:02
  • You don't have to accept answer, but to appreciate people who are trying to help you. – CodeWizard Jan 10 '16 at 04:03
  • @codeWizard Again, I appreciate you taking interest. The downvote does not mean I do not like you or anything, simply that I disagree with the answer. If you think it is correct, convince me so. – PyRulez Jan 10 '16 at 04:08
  • It cant be done. period. git does not trace folders like svn it track content. http://steindom.com/articles/add-empty-directory-git-repository I will be glad to lean how you can do it, but you cant – CodeWizard Jan 10 '16 at 04:11
  • @codeWizard Using internal commands, it is revealed that it does, for example, track folders and files separately. Unless I have made some error in my Note. Also see torek's comment. – PyRulez Jan 10 '16 at 04:15

2 Answers2

-1

In Git, you cannot commit empty folders, because Git does not actually save folders, only files. You'll have to create some placeholder file inside those directories if you actually want them to be "empty" (i.e. you have no committable content). for more info and your answer Commit empty folder structure (with git)

Community
  • 1
  • 1
-2

The answer to "How can I add an empty directory to a Git repository?" is you can't through regular git commands, because the staging only has files, not folders.

You can do it if you will add empty file usually named .gitkeep to your folder so git will track this folder since it has content.

# go to the desired folder    
cd <folder>

# Create empty file 
touch .gitkeep

# git status will now show the file so you can add the folder to git

Git does not track files !!! it track content regardless of its location, so empty folder does not have any content to track. in order to track them they must have content.

.gitkeep is just a placeholder.
A dummy file, so git will not forget about the directory, since git tracks only files and not directories.


As you can see here git does not care what is the name of the file or where its located. It store the metadata in a different location then the file.

enter image description here

CodeWizard
  • 128,036
  • 21
  • 144
  • 167