tags:

views:

371

answers:

3

Okay, I'm new to git, coming from SVN.

I've got my repo @ github. Last night, did some work at home and pushed it to github. It involved some deleting of files and directories. Now I'm on my work box, which had a copy of the code before deleting the files and directories.

I issued the following:

git remote update
git checkout HEAD
git pull origin HEAD

It deleted all of the files it should have, but not the directories the files were in.

Two questions: why did it not remove the directories and is there a git command I can issue in the current state to remove them?

A: 

Git does not track directories currently (see git wiki), that is, you neither can add empty directories nor will git remove directories that end up empty. (EDIT: Thanks, Manni, I was wrong! You can't add empty directories, but git will remove directories that become empty because their tracked content was deleted.)

As to the command to remove the empty directories: that depends on your operating system.

For Linux you could use, e.g.,

find -depth -type d -empty -exec rmdir {} \;

However, this will remove all empty directories!

janko
The wiki page says that git doesn't create empty directories for you. It does **not** say that it won't delete empty directories. What mculp wants works for me.
innaM
+1  A: 

As part of most operations that alter the working tree (pull, merge, checkout, etc.) git will remove any directories which are made empty by that operation (i.e. git removed the last file).

git won't remove any directories that aren't completely empty, so if you have hidden or ignored files then just because git removes the last tracked file from that directory doesn't necessarily mean that git will be able to remove that directory. git doesn't consider this to be an error condition so won't complain about it.

Charles Bailey
My GitHub repo does not have the directories. When I committed, it removed the directories correctly. However, when I checkout/pull it removes only the files. The directories are empty.$ ls -altotal 8drwxr-xr-x 2 mculp mculp 4096 Sep 23 15:43 ./drwxr-xr-x 8 mculp mculp 4096 Sep 30 10:51 ../
mculp
I'm not quite sure what you mean by "switched HEAD to master", I presume you mean "`git checkout master`" but most people would just say "I switched to/checked out master". Anyway, when git said 'Already up-to-date.' it means that it didn't do anything. git will only remove directories that it *makes* empty.
Charles Bailey
Bad wording. I changed "HEAD" to "master" as some people have suggested in the comments.
mculp
+4  A: 

Git doesn't track directories, so it won't remove ones that become empty as a result of a merge or other change. However, you can use git clean -fd to remove untracked directories (the -fd flag means force removal of untracked files and directories).

mipadi
I'm confused. Then why did the directories get deleted from my GitHub repo when I committed/pushed changes?
mculp
The directories in your working copy may have contained untracked files (including hidden untracked files), and thus they may appear to be empty but really aren't, so Git didn't remove them. `git clean` will, of course.
mipadi
Thanks for the command. That cleaned them out. However, I still don't understand why it deleted the directories on a commit/push but not on a checkout/pull. The directories contained no hidden or unhidden files.
mculp
It may have something to do with the fact that GitHub repos are _bare_ repositories (they don't have working copies), but your local one does. I imagine if you cloned a _new_ repo from the GitHub origin, you wouldn't have those directories.
mipadi