Git:使用 git clean 排除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11085795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:04:43  来源:igfitidea点击:

Git: Exclude a file with git clean

gitversion-controlgit-clean

提问by santiagobasulto

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 -Xflag of git clean would remove untracked files. As you can imagine, i'm not tracking .pycnor *~files. And that would make the trick. The problem is that i've a local_settings.pyfile that I'd like to keep after the git clean.

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

So, this is what I've got.

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

.gitignore:

.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

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

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.

我不想删除 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"

And nothing seems to work.

似乎没有任何效果。

EDIT:

编辑:

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

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

采纳答案by Rifat

Will you please try that with small xinstead of the capital one. Like - git clean -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)

From the git documentation:

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.
   -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.

回答by forivall

git clean -X -n --exclude="!local_settings.py"

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

作品。我在谷歌搜索并获得此页面时发现了这一点

回答by Jonah Graham

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/info/exclude(例如我的 IDE 项目文件)中。他们可以像这样进行清理:

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

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.

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

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

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

回答by Chris Pratt

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

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

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 -Bparameter when you call python, e.g.

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

python -B manage.py runserver

回答by Christoph Eberhardt

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

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

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

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

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

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