45

我正在开发一个大型 python 项目,如果 .pyc 和 *~ 文件我真的很恶心。我想删除它们。我已经看到-Xgit clean 的标志会删除未跟踪的文件。正如你可以想象的那样,我没有跟踪.pyc也没有*~文件。这将成为诀窍。问题是我有一个local_settings.py文件要在 git clean 之后保留。

所以,这就是我所拥有的。

.gitignore:

*.pyc
*~
local_settings.py

当我执行这个命令时:

git clean -X -n -e local_settings.py

我得到这个结果列表:

将删除 local_settings.py
将删除 requirements.txt~
将删除(其他一堆)~ 文件
将删除(其他一堆)pyc 文件

我不想删除 local_settings.py 文件。我已经尝试了很多方法来做到这一点,但我无法弄清楚如何完成它。

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"

似乎没有任何效果。

编辑:

对于后代,正确的做法是(感谢@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)
4

5 回答 5

36

区别在于X您使用的资本。使用小x而不是大写。像在: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)

git 文档

   -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.
于 2012-06-18T15:33:40.480 回答
26
git clean -X -n --exclude="!local_settings.py"

作品。当我搜索并获得此页面时,我发现了这一点。

于 2013-06-17T21:10:57.930 回答
4

我将属于此类的本地文件放在 .git/info/exclude 中(例如我的 IDE 项目文件)。他们可以像这样进行清洁:

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

在哪里:

  • --others:显示未跟踪的文件
  • --exclude-from:提供标准的 git 忽略样式文件以从列表中排除
  • -z / -0:使用 \0 而不是 \n 来拆分名称
  • --no-run-if-empty:如果列表为空,则不运行 rm

您可以创建一个别名,例如:

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

--interactive 意味着您必须执行 git myclean -f 才能强制删除。

参考: http: //git-scm.com/docs/git-ls-files(加上默认的.git/info/exclude的第一行)

于 2015-03-07T13:49:19.053 回答
0

如果您正在运行 Python 2.6+,只需将环境变量 , 设置PYTHONDONTWRITEBYTECODEtrue. 您可以将以下内容添加到类似的内容中,.profile或者.bashrc为您的个人资料完全禁用它:

export PYTHONDONTWRITEBYTECODE=true

或者,如果您只想为您正在工作的特定项目执行此操作,则每次都需要在您的 shell 中运行上述代码(或者如果您使用的是 virtualenv 和 virtualenvwrapper,则在您的一个 virtualenv 初始化脚本中),或者您可以-B在调用时简单地传递参数python,例如

python -B manage.py runserver
于 2012-06-18T15:29:39.963 回答
0

如果您已经提交了 pyc 等,请执行以下操作:

将 *.pyc、*~ 和 local_settings.py 添加到 .gitignore。然后在您的 git 存储库中执行以下操作:

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

然后做:

git commit -am "get rif of them"

现在他们不应该再打扰你了

于 2012-06-18T15:36:58.157 回答