Git: Exclude a file with git clean

白昼怎懂夜的黑 提交于 2019-11-30 06:07:55
Rifat

Will you please try that with small x instead of the capital one. Like - 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.
git clean -X -n --exclude="!local_settings.py"

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

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)

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!