I deleted all files from a directory in my work folder, but the directory itself still exists. After pushing the changes to a remote repository (GitHub) I checked my project and noticed that the directory was gone. Is it possible that Git deletes empty folders?
Why is the directory not shown?
Git does not track directories; it only tracks files.
If there are no files in a directory, that directory does not “exist” to Git when adding or removing files. Particularly, a directory will disappear from Git's index when you've deleted all files from it and add that change to the index. Vice-versa, a directory will not be added via
git add if it's empty.
In other words: If you can see the directory locally in your file browser, but it disappeared from GitHub, you most likely removed all files from the directory, added that change to the index, and committed and pushed it.
How do I track an empty directory, then?
If you want to explicitly track an empty directory, you have to create a file in it. Since Git won't track empty directories, you have to trick it into doing so by adding a file in the directory to Git's index.
Usually, people store a file called
.gitkeep in a directory that they wish to track, but where the directory should stay empty for the time being. You can give the file any other name, of course, but the name
.gitkeep is a convention. The
.gitkeep file (due to starting with a
.) will not be shown by file listings on most systems.
.gitkeep, some users also like to put a
README file there instead, ideally with a short description of why the directory has to exist in the first place.
$ mkdir foo $ git init $ git add . $ git ls-files # which files does Git know about? # apparently, none $ touch foo/bar # create a file in the directory $ git add . $ git ls-files # does Git know about it now? foo/bar # yep!
foo directory only gets added to the index once a file is in it.
What if I really want to track an empty directory?
Add the following
.gitignore file to the folders you wish to include in your repo.
# Ignore everything in this directory * # Except this file !.gitignore
.gitkeep works but isn’t an official solution. See https://stackoverflow.com/questions/7229885/what-are-the-differences-between-gitignore-and-gitkeep
Git does not track folders. In order to refrain from accumulating messes, when the index changes from (indirectly) referencing a directory to not referencing it (by doing something like
git rm on the last registered file in the directory), Git tries removing the directory before it loses track of it by it no longer being in the index. If Git is successful because the directory has indeed become empty (and does not contain unregistered files), the directory will be gone, if not, it will stick around since Git no longer has notice of it.
Git might delete folders if you ran
git clean with
-dffx (possibly even with a subset of those options).
As @TRiG mentions in the comments, if the directory is empty when you commit, it won’t show up in, e.g., GitHub, because git doesn’t track directories (it track files)—the tree where those files were is no longer relevant because there are no files, so there is no tree.
Without more detail, it’s practically impossible for us to properly answer your underlying questions (what happened?).