11
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 ?

Hanlin
  • 835
  • 10
  • 26
  • Weird. docs say *Cleans the working tree by **recursively** removing files that are not under version control, starting from the current directory.* – Tim Aug 26 '15 at 07:53
  • A wild guess: Git only traverses directories in the index with the `-X` option, ignoring the `temp` directory (because it initially doesn't know it, only after you add a file). I can replicate the issue with Git 2.1.4 on Ubuntu. Why don't you ask this at git@vger.kernel.org? – krlmlr Aug 26 '15 at 08:43

1 Answers1

10

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$

Saverio Proto
  • 1,085
  • 9
  • 20
  • but `-x` also removing all untracked files – Hanlin Aug 29 '15 at 03:19
  • Ok I see you point. You want to delete all files that match the .gitignore but do not delete all the other untracked files. I think this problem you have is really a corner case due to the fact that git does not track empty folders. – Saverio Proto Sep 04 '15 at 12:56
  • I'm working on a CLI called gnore that should solve this, as well as add a bunch of other goodies – Clay Risser Aug 22 '17 at 04:57