6

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.

Matt W
  • 9,407
  • 17
  • 83
  • 146

2 Answers2

14

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

Tarun Kumar
  • 1,538
  • 11
  • 17
Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
1

git clean will not remove files that are being ignored by git by default.

Add the -x flag to remove ignored files as well or the -X flag to remove only ignored files and directories

git clean -fdx
git clean -fdX
LightBender
  • 3,534
  • 1
  • 10
  • 26