57

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)
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
santiagobasulto
  • 11,320
  • 11
  • 64
  • 88

5 Answers5

47

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.
Abel
  • 56,041
  • 24
  • 146
  • 247
Rifat
  • 7,628
  • 4
  • 32
  • 46
  • 3
    with `-X` you must prefix path with `!` (as suggested by @forivall) – Yukulélé Oct 13 '18 at 07:36
  • 1
    I need `-d` as well, otherwise, directories won't be removed. – Polv Aug 15 '20 at 10:34
  • Sometimes there are reasons to use the uppercase `-X`, so the correct answer is to use `!` to invert the exclusion. In Bash, the exclamation mark has to be escaped with a backslash: `\!`. – ADTC Feb 26 '23 at 21:03
35
git clean -X -n --exclude="!local_settings.py"

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

forivall
  • 9,504
  • 2
  • 33
  • 58
  • 3
    Nice! Thank you. :) I had to leave off the `!` in my shell, but it worked. – Vimes Nov 03 '16 at 17:22
  • 4
    In my case I had to escape the exclamation mark, so this worked for me to delete everything except the folder called ".idea": `git clean -X -n -e \!.idea` – CGodo Jul 16 '19 at 17:22
  • 1
    @kuropan it worked in 2013 – forivall Oct 16 '21 at 22:32
  • 2
    Aha! Now I got it. The `-X` also inverts the behavior of the `-e` option! Which kind of makes sense. This is then why we need the `!` (I have removed my earlier comment to avoid confusion) – kuropan Oct 18 '21 at 07:37
  • Thank you so much, this saved my day! Is this an undocumented feature? The link above is dead and the `!` symbol is nowhere to be found on the git clean man page. Did I miss something? – jkokorian Oct 11 '22 at 10:35
  • @jkokorian, the usage of the exclamation point is documented with `gitignore`: https://git-scm.com/docs/gitignore – irowe Nov 16 '22 at 19:27
5

I put local files that fall into this category in .git/info/exclude (e.g. my IDE project files). They you can do a clean like this:

git ls-files --others --exclude-from=.git/info/exclude -z | \
    xargs -0 --no-run-if-empty rm --verbose

Where:

  • --others: shows untracked files
  • --exclude-from: provide a standard git ignore style file to exclude from the list
  • -z / -0: use \0 instead of \n to split names
  • --no-run-if-empty: Don't run rm if list is empty

You could create an alias, e.g.:

git config --global alias.myclean '!git ls-files --others --exclude-from=.git/info/exclude -z | xargs -0 --no-run-if-empty rm --verbose --interactive'

The --interactive means you have to do git myclean -f to force the deletion.

Reference: http://git-scm.com/docs/git-ls-files (plus the first line of the default .git/info/exclude)

Jonah Graham
  • 7,890
  • 23
  • 55
  • 1
    Great answer! On my Mac this command worked for me: `git config alias.myclean '!git ls-files --others --exclude-from=.git/info/exclude | xargs -I {} rm -v {}'`. Also a word of warning against `--interactive`: I once aliased `rm` to `rm --interactive` in my bashrc and got so fed up of the constant prompts for removal, after a while just added `-f` by default. Now I can barely type `rm` without my muscle memory adding `-f`, even though I long since got rid of the alias. My original plan completely backfired. I'm doomed to forever automatically perform a more dangerous version of `rm`. – biddlesby May 11 '18 at 09:06
0

If you're running Python 2.6+ just set the environment variable, PYTHONDONTWRITEBYTECODE, to true. You can just add the following to something like .profile or .bashrc to disable it entirely for your profile:

export PYTHONDONTWRITEBYTECODE=true

Or, if you just want to do that for a particular project you're working, you'll need to run the above in your shell each time (or in one of your virtualenv init scripts if you're using virtualenv and virtualenvwrapper), or you can simply pass the -B parameter when you call python, e.g.

python -B manage.py runserver
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
0

If you have commited the pyc's and so on already, do the following:

Add *.pyc, *~ and local_settings.py to the .gitignore. Then do in your git repository:

find . -name '*.pyc' | xargs rm
find . -name '*~' | xargs rm

then do:

git commit -am "get rif of them"

now they shouldn't bother you anymore