Git checkout/pull doesn't remove directories?

GitGit Clean

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmculpView Question on Stackoverflow
Solution 1 - GitmipadiView Answer on Stackoverflow
Solution 2 - GitYogurtuView Answer on Stackoverflow
Solution 3 - GitCB BaileyView Answer on Stackoverflow
Solution 4 - GitqwertzguyView Answer on Stackoverflow
Solution 5 - GitjankoView Answer on Stackoverflow