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 file that I’d like to keep after the git clean.

So, this is what I’ve got.



When I execute this command:

git clean -X -n -e

I get this list of results:

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

I don’t want to remove the 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
git clean -X -n -e ""
git clean -X -n
git clean -X -n --exclude=""

And nothing seems to work.


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

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


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 # Shows what would remove (-n flag)
git clean -x -f -e # Removes it (note the -f flag)

From the git documentation:

       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.

       Remove only files ignored by git. This may be useful to rebuild
       everything from scratch, but keep manually created files.

Answered By – Rifat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published