忽略git存储库中的.pyc文件


107

如何忽略.pycgit中的文件?

如果我把它放在.gitignore里面是行不通的。我需要将它们取消跟踪,而不要检查提交。


4
.gitignore应该可以工作。您是否可以提供已放入.gitignore的行的副本来尝试解决此问题?
Al Riddoch

Answers:


41

放进去.gitignore。但是从gitignore(5)手册页:

  ·   If the pattern does not contain a slash /, git treats it as a shell
       glob pattern and checks for a match against the pathname relative
       to the location of the .gitignore file (relative to the toplevel of
       the work tree if not from a .gitignore file).

  ·   Otherwise, git treats the pattern as a shell glob suitable for
       consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in
       the pattern will not match a / in the pathname. For example,
       "Documentation/*.html" matches "Documentation/git.html" but not
       "Documentation/ppc/ppc.html" or
       "tools/perf/Documentation/perf.html".

因此,要么指定适当*.pyc条目的完整路径,要么将其放置在.gitignore从存储库根目录开始(包含在内)的任何目录中的文件中。


6
为了避免让其他人感到困惑,Ignacio对手册页的解释是错误的。您无需将* .pyc放在同一目录中,将其放在父目录(或祖父母目录等)中就足够了。
Godsmith

@Godsmith:已更正。
Ignacio Vazquez-Abrams

241

您应该添加以下行:

*.pyc 

.gitignore库初始化之后就在你的git仓库树的根文件夹中的文件。

正如ralphtheninja所说,如果您忘记事先做,只要将行添加到.gitignore文件中,所有先前提交的.pyc文件仍将被跟踪,因此您需要将它们从存储库中删除。

如果您使用的是Linux系统(或MacOSX等“父级”),则只需使用以下一条从存储库根目录执行的命令即可快速完成此操作:

find . -name "*.pyc" -exec git rm -f "{}" \;

这只是意味着:

从我当前所在的目录开始,查找名称以extension结尾的所有文件.pyc,并将文件名传递给命令git rm -f

之后*.pyc从混帐作为跟踪文件的文件删除,提交此变更到仓库,然后你就可以在最后加*.pyc一行到.gitignore文件中。

(改编自http://yuji.wordpress.com/2010/10/29/git-remove-all-pyc/


3
另外,只要从git和本地计算机中删除文件,就可以git rm --cached *.pyc从顶层目录中删除。从这里
Anupam

我有它存在,但是一旦你无论如何提交它得到通过的git跟踪的pyc文件,你应该首先将它们删除,并在提交的情况下,你有他们在仓库
马哈茂德Hboubati

84

在放入之前,您可能已将它们添加到存储库*.pyc.gitignore
首先从存储库中删除它们。


3

我尝试使用先前文章的句子,并且不递归工作,然后阅读一些帮助并获得以下内容:

find . -name "*.pyc" -exec git rm -f "{}" \;

要在.gitignore文件中添加* .pyc以保持git干净,必须使用pd

echo "*.pyc" >> .gitignore

请享用。


0

感谢@Enrico的答案。

请注意,如果您使用的是virtualenv,则.pyc您当前所在的目录中还会有几个文件,这些文件将由他的find命令捕获。

例如:

./app.pyc
./lib/python2.7/_weakrefset.pyc
./lib/python2.7/abc.pyc
./lib/python2.7/codecs.pyc
./lib/python2.7/copy_reg.pyc
./lib/python2.7/site-packages/alembic/__init__.pyc
./lib/python2.7/site-packages/alembic/autogenerate/__init__.pyc
./lib/python2.7/site-packages/alembic/autogenerate/api.pyc

我想删除所有文件是没有害处的,但是如果您只想删除.pyc主目录中的文件,则只需执行

find "*.pyc" -exec git rm -f "{}" \;

这将仅从app.pycgit存储库中删除文件。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.