Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Exclude a file with git clean

i'm working on a big python project, and i'm really sick if .pyc and *~ files. I'd like to remove them. I've seen that the -X flag of git clean would remove untracked files. As you can imagine, i'm not tracking .pyc nor *~ files. And that would make the trick. The problem is that i've a local_settings.py file that I'd like to keep after the git clean.

So, this is what I've got.

.gitignore:

*.pyc *~ local_settings.py 

When I execute this command:

git clean -X -n -e local_settings.py 

I get this list of results:

Would remove local_settings.py
Would remove requirements.txt~
Would remove (other bunch of) ~ files
Would remove (other bunch of) pyc files

I don't want to remove the local_settings.py file. I've tryed lots of ways to do it, but i can't figure out how to acomplish it.

git clean -X -n -e local_settings.py git clean -X -n -e "local_settings.py" git clean -X -n --exclude=local_settings.py git clean -X -n --exclude="local_settings.py" 

And nothing seems to work.

EDIT:

For posterity, the right way to do it is (Thanks @Rifat):

git clean -x -n -e local_settings.py # Shows what would remove (-n flag) git clean -x -f -e local_settings.py # Removes it (note the -f flag) 
like image 728
santiagobasulto Avatar asked Jun 18 '12 15:06

santiagobasulto


People also ask

Does git clean remove files in Gitignore?

gitignore are not being tracked, you can use the git clean command to recursively remove files that are not under version control.

Does git clean delete files?

git clean deletes ignored files only if you use either the -x or -X option, otherwise it just deletes untracked files.

Does git clean respect Gitignore?

Git normally doesn't clean ignored files unless the -x flag is specified, but strangely it cleans out when configured as you did ( folder/* ). As @VonC pointed out, you should change your . gitignore -file to ignore the directory ( data/ ) rather than its contents ( data/* ).


2 Answers

The difference is the capital X you're using. Use a small x instead of the capital one. Like in: git clean -x.

git clean -x -n -e local_settings.py # Shows what would remove (-n flag) git clean -x -f -e local_settings.py # Removes it (note the -f flag) 

From the git documentation:

   -x        Don't use the standard ignore rules read from .gitignore (per        directory) and $GIT_DIR/info/exclude, but do still use the ignore        rules given with -e options. This allows removing all untracked        files, including build products. This can be used (possibly in        conjunction with git reset) to create a pristine working directory        to test a clean build.     -X        Remove only files ignored by git. This may be useful to rebuild        everything from scratch, but keep manually created files. 
like image 175
Rifat Avatar answered Sep 28 '22 18:09

Rifat


git clean -X -n --exclude="!local_settings.py" 

works. I discovered this when I googled and got this page.

like image 44
forivall Avatar answered Sep 28 '22 17:09

forivall