25

I read the great git tutorial here, I'm unable to create directories.

My folder structure is:

Code - Python - C++ - F# - ...

From within a initialised local repository "Code" I changed into the subfolders "Python", "C++", ... did git init and now I want the same structure on my versioning server.

% git commit -m "added directories"
# On branch master
nothing to commit (working directory clean)

I guess I got something completely wrong, did I?

Thanks, wishi

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
wishi
  • 7,188
  • 17
  • 64
  • 103

3 Answers3

41

Try to add some files to directories first. You can add some dummy file to directory, and remove that file when you have added the actual content for the directory.

Git does not track empty directories, it tracks files and their contents.

git init
mkdir foo
touch foo/dummy_empty_file

git add foo
git add foo/dummy_empty_file
git commit -m "adding files"
Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
  • 3
    You only need one of the two adds here. When given a directory, `git-add` recursively adds it. On the other hand, adding the only file in the directory is the same as adding the entire directory, since as you say, git tracks files and their contents. – Cascabel Oct 15 '09 at 17:45
  • 3
    In Rails the convention is to put a `.keep` file in the directory. This seems like a reasonable (de facto) standard to me and I'd suggest you use that filename. – Nate May 22 '14 at 14:06
  • In many other projects, I see `.gitkeep`, similar to `.gitignore` but just a dummy file to check-in for keeping. – Joe Sadoski May 16 '22 at 15:11
  • Use git add foo -f if the folder you try to add is in .gitignore. – Nițu Alexandru Oct 30 '22 at 09:18
31

Git won't commit empty directories. There must be files in the directories first.

Marc W
  • 19,083
  • 4
  • 59
  • 71
  • 2
    But this "file" could be an invisible .gitignore file, see http://stackoverflow.com/questions/115983/how-do-i-add-an-empty-directory-to-a-git-repository – Design by Adrian Oct 28 '14 at 18:09
1

Create an empty .gitignore file inside the empty folder that you would like to commit.

Git will only track Files and the changes in them. So folders are tracked as part of the file changes in them. In order to create an empty folder and commit it, there have to be some files inside it.

You can add any file you want, if you don't have any files to put there, I would suggest using an empty .gitignore file. This will have no effect on your repo as long as it's empty

ABHi
  • 404
  • 4
  • 8