git clean does not clean unwanted folder


I have a .NET solution which, after switching back to master branch, leaves a project folder behind. I want to clean this folder up (there is nothing but bin and obj folders inside, with some old dll’s) so I tried git clean -f -d. This did not work.

I checked which files are being tracked by git using git ls-tree test/flat -r --name-only and sure enough the project folder to be cleaned is not listed.

What should I do to convince git to clean the working folder so that it’s contents is only what is in the master branch?

Note: The unwanted project folder is also not ignored.


If there are ignored files in that folder, git clean won’t remove the folder.

The options you want to look into, possibly using all three of them are:

  • -f: force git clean to delete the files
  • -d: delete untracked directories in addition to untracked files
  • -x: delete files that are ignored by git

This command:

git clean -fdx

Should remove all untracked files and directories.

NOTE! And I say this because many doesn’t read the warning labels, if you do this, you can’t get those files back. They’re gone. You will need to have disk- or file-level backup other than git to restore those files.

Use “git clean -ifdx” to delete files and directories interactively. This way you can avoid accidental delete of a file. Git shows you the list of file that will be deleted and asks you to choose from the option.

enter image description here

Answered By – Lasse V. Karlsen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published