Git: Exclude a file with git clean

前端 未结 5 1024
灰色年华
灰色年华 2020-12-14 14:45

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

相关标签:
5条回答
  • 2020-12-14 15:05

    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

    0 讨论(0)
  • 2020-12-14 15:07

    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.
    
    0 讨论(0)
  • 2020-12-14 15:12

    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
    
    0 讨论(0)
  • 2020-12-14 15:17

    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)

    0 讨论(0)
  • 2020-12-14 15:24
    git clean -X -n --exclude="!local_settings.py"
    

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

    0 讨论(0)
提交回复
热议问题