Git checkout / pull not deleting directories?

I have a @github repo. I did a bit of work at home and pushed it on github. This is due to the deletion of files and directories. Now I am in my working window, which had a copy of the code before deleting files and directories.

I posted the following:

git remote update git checkout HEAD git pull origin HEAD 

He deleted all the files that he should have, but not the directories in which the files were.

Two questions:

  • Why doesn't he delete directories?
  • Is there a git command that I can execute in the current state to remove them?
+76
git git-clean
Sep 30 '09 at 16:11
source share
4 answers

Git does not track directories, so it does not delete those that become empty as a result of merging or other changes. However, you can use git clean -fd to delete non-playable directories (the -fd flag means force the removal of unprepared files and directories).

+146
Sep 30 '09 at 18:19
source share

As part of most operations that modify the working tree (pull, merge, checkout, etc.), git will delete any directories that were made empty with this operation (i.e. git deleted the last file).

git will not delete any directories that are not completely empty, so if you have hidden or ignored files, just because git removes the last tracked file from this directory does not necessarily mean that git will be able to delete this directory. git does not consider this a condition for error, so it will not complain about it.

+3
Sep 30 '09 at 17:09
source share

Git does not track directories, files (their path). Git creates all directories for these paths if they do not already exist (cool!), However it does not delete them if all the files contained in the path are moved or deleted (not cool ☹ ... but there are reasons).

Solution (after you pulled / redirected / merged):

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

If you do not stash before clean , you will lose all your raw files (irreversibly).

Note. Since this also clears all ignored files, you may need to run some of your build scripts again to recreate the project metadata (for example: ./gradlew eclipse ). It also removes directories that are empty and that were never part of the paths to Git files.

+2
Jun 22 '15 at 21:46
source share

Git does not track directories at present (see the Git wiki ), that is, you cannot add empty directories and git will not delete directories that end up empty. (EDIT: Thank you, Manny, I was wrong! You cannot add empty directories, but git will delete directories that become empty because their tracked content has been deleted. )

Regarding the command to delete empty directories: it depends on your operating system.

For Linux, you can use, for example,

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

However, this will delete all empty directories!

-one
Sep 30 '09 at 16:23
source share



All Articles