101

I've got my repo @ github. I 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:

  1. Why did it not remove the directories?
  2. Is there a git command I can issue in the current state to remove them?
3
  • Agreed, git checkout HEAD does nothing, since HEAD is a ref pointing to the currently checked-out commit. You probably were going for master in both cases.
    – Cascabel
    Commented Sep 30, 2009 at 17:52
  • Well, HEAD actually updated everything with the exception of deleting the empty directories. Like I said, I'm new to git.
    – mculp
    Commented Sep 30, 2009 at 18:13
  • Possible duplicate of How do I force "git pull" to overwrite local files? Please check the answers there if you still want some different solutions.
    – DrBeco
    Commented Mar 31, 2017 at 5:10

6 Answers 6

180

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).

11
  • 4
    I'm confused. Then why did the directories get deleted from my GitHub repo when I committed/pushed changes?
    – mculp
    Commented Sep 30, 2009 at 18:25
  • 2
    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
    Commented Sep 30, 2009 at 18:37
  • 4
    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
    Commented Sep 30, 2009 at 18:55
  • 28
    But of course, run the command with -n (dry run) instead of -f first, so you can see what is going to be deleted. Especially since git clean -d removes not only those worisome directories, but also untracked files.
    – Todd Owen
    Commented Jan 13, 2012 at 8:16
  • 2
    @EliGolin: git clean doesn't delete files listed in .gitignore unless you pass the -x option.
    – mipadi
    Commented Jun 20, 2016 at 19:50
10

I had same issue, in my case on build service (CI).. as GIT pulls all files without cleaning folders, all the bin / obj that were previously builded by the CI are dirty, so If I remove a test project, the bin will still contain the DLL and will mention tests that does not exist.

In order to solve this issue; this command seems to do the trick (at least for me)

git clean -fd -x

where X will remove all untracked files:

-X Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.

1
  • 2
    Be careful with this ... the -x cleared my logs which I did want ( locally on remote server, just not in Git ) but are ignored by .gitignore because they change often
    – Ricky Levi
    Commented Jun 13, 2023 at 11:54
4

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.

5
  • 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 -al total 8 drwxr-xr-x 2 mculp mculp 4096 Sep 23 15:43 ./ drwxr-xr-x 8 mculp mculp 4096 Sep 30 10:51 ../
    – mculp
    Commented Sep 30, 2009 at 18:10
  • 1
    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.
    – CB Bailey
    Commented Sep 30, 2009 at 18:33
  • Bad wording. I changed "HEAD" to "master" as some people have suggested in the comments.
    – mculp
    Commented Sep 30, 2009 at 18:40
  • @CharlesBailey, what you said is not what I see. git checkout did not remove a completely empty directory.
    – Asclepius
    Commented Oct 25, 2013 at 18:22
  • @A-B-B: Git only removes directories that are made empty by a checkout operation. I have tested this again; it still works.
    – CB Bailey
    Commented Oct 26, 2013 at 8:05
2

Git doesn't track directories, files (with their path). Git creates all the directories for those paths if they don't exist yet (cool!), however it does not delete them if all the files contained in a path are moved or deleted (not cool ☹ ... but there's reasons).

Solution (once you have pulled / fast-forwarded / merged):

git stash --include-untracked
git clean -fd
git stash pop

If you don't stash before clean, you will loose all your untracked files (irreversibly).

Note: since this cleans all ignored files too, you might need to run some of your build scripts again to recreate project metadata (ex: ./gradlew eclipse). This also delete directories that are empty and that were never part of paths of git files.

0

For me, the directory was a submodule, so I needed to run -f twice:

git clean -f -f -d
-1

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!

2
  • 1
    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
    Commented Sep 30, 2009 at 16:27
  • @janko, Actually, git failed to remove a completely empty directory for me.
    – Asclepius
    Commented Oct 25, 2013 at 18:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.