Git bash giving error of warning: unable to rmdir

This happens mostly when your files are still in use. I personally experienced this sometimes with atom editor. Try to close your editor, and maybe any running compilers that are watching for changes too, and try to checkout again.


warning: unable to rmdir on git checkout means that you're switching to a branch where this directory doesn't exist; git tries to remove it but there are some temporary files that git cannot remove; the directory is not empty so git cannot remove it also, hence the warning.


Found the answer, instead of git checkout, use git checkout --recurse-submodules.

Use git config submodule.recurse true to tell git always use --recurse-submodules (only in git versions 2.14+), add --local if you want that only in local project level.

Reason: This issue happens on (Git < 2.13) when git checkout could not take care of those submodules correctly.

Reference: https://github.com/gitextensions/gitextensions/issues/2966#issuecomment-622666568


Original Answer

Actually I think this answer is partially right :O

If a folder is tracked by local .git within that folder, it would be changed according to .git when you switch branches (e.g. deleted from our point of view, if the other branch does not have this folder).

If a folder is ignored by .gitignore, the folder would be left unchanged when you switch branches.

However, if the folder is a submodule, which is tracked by submodule .git, local .git would try use rmdir when switching branches, which caused the problem.