Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git clean does not clean unwanted folder

Tags:

git

master

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.

like image 789
Matt W Avatar asked Aug 30 '17 11:08

Matt W


2 Answers

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

like image 182
Lasse V. Karlsen Avatar answered Sep 30 '22 08:09

Lasse V. Karlsen


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
like image 28
LightBender Avatar answered Sep 30 '22 06:09

LightBender