2011-04-05 85 views
50

我该如何忽略git中的.pyc文件。 如果我放入.gitignore不起作用:我需要他们不追踪,不检查它的提交。忽略git仓库中的.pyc文件

+4

.gitignore应该工作。你能否提供你在.gitignore中放入的行的副本来尝试解决这个问题? – 2011-04-05 11:47:46

回答

27

把它放在.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文件中的任何从库根领先的目录(含)。

+3

为了避免让其他人感到困惑,Ignacio对手册页的解释是错误的。您不需要将* .pyc放在同一个目录中,只需将它放在父目录(或祖父母等)中即可。 – Godsmith 2015-06-02 08:21:22

+0

@Godsmith:更正。 – 2015-06-02 08:23:22

62

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

*.pyc 

.gitignore文件在你的git仓库树的根文件夹:

+26

这个问题每次都会让我知道 – ecoe 2013-10-13 18:54:12

115

你应该加一行。

由于ralphtheninja说,如果你忘了做这一点,事前,如果你只需要添加行至.gitignore文件,所有以前犯.pyc文件仍将被跟踪,所以你需要从删除库。

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

如果你是一个Linux系统(或“父母&儿子”像MacOSX的),你可以快速地与眼前这个,你需要从库的根执行一个命令行的做

这也就意味着:

从我目前的目录开始,查找其 名与扩展.pyc结束的所有文件,并通过文件名的命令git rm -f

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

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

+0

这真是太棒了 – Tunn 2017-12-18 23:52:58

+0

另外,只需从git和本地机器上删除文件,就可以从顶层目录执行'git rm --cached * .pyc'。从[这里]得到它(https://coderwall.com/p/wrxwog/why-not-to-commit-pyc-files-into-git-and-how-to-fix-if-you-already-did ) – Anupam 2017-12-28 05:42:58

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 "{}" \;

这将从git存储库中删除app.pyc文件。