Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-clean

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 ?

like image 805
Hanlin Avatar asked Aug 26 '15 07:08

Hanlin


People also ask

Does git clean remove files in Gitignore?

git clean -X will remove any files matching the patterns in . gitignore . The -n at the end causes it to not actually remove files, only to dry run, reporting files that would have been removed. Add -d to also remove ignored directories.

How do you remove files that are listed in the .gitignore but still on the repository?

gitignore are not being tracked, you can use the git clean command to recursively remove files that are not under version control. Use git clean -xdn to perform a dry run and see what will be removed. Then use git clean -xdf to execute it. Basically, git clean -h or man git-clean (in unix) will give you help.


1 Answers

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$

like image 171
Saverio Proto Avatar answered Sep 21 '22 16:09

Saverio Proto