How to use git clean remove all file list in .gitignore?

Hanlin picture Hanlin · Aug 26, 2015 · Viewed 7.8k times · Source
mkdir repo
cd repo
git init
mkdir temp
touch temp/in.tmp
touch out.tmp
echo '*tmp' > .gitignore

when use git clean -Xn it show Would remove out.tmp

but I want to remove temp/in.tmp together

git clean -Xdn not work too, but cd to temp directory and run git clean -Xn it show Would remove in.tmp

so I wonder is there a command to remove all file list in .gitignore include subdirectory, in this case how to use git clean remove temp/in.tmp and out.tmp

$ git --version
git version 1.9.5.msysgit.1

I found something weird

add a file in temp directory and stage it , git clean -Xn seems work

touch temp/a.txt
git add temp/a.txt
git clean -Xn

it show

Would remove out.tmp
Would remove temp/in.tmp

but why that happend ?

Answer

Saverio Proto picture Saverio Proto · Aug 28, 2015

You need -x because you want to delete file ignored by .gitignore

git clean -fdx

will successufully clean everything for you

macsp:repo proto$ git clean -fdxn Would remove .gitignore Would remove out.tmp Would remove temp/ macsp:repo proto$