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

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

如何忽略.pycgit中的文件?

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

How can I ignore .pyc files in git?

If I put it in .gitignore it doesn’t work. I need them to be untracked and not checked for commits.


回答 0

放进去.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从存储库根目录开始(包含在内)的任何目录中的文件中。

Put it in .gitignore. But from the gitignore(5) man page:

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

So, either specify the full path to the appropriate *.pyc entry, or put it in a .gitignore file in any of the directories leading from the repository root (inclusive).


回答 1

您应该添加以下行:

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

You should add a line with:

*.pyc 

to the .gitignore file in the root folder of your git repository tree right after repository initialization.

As ralphtheninja said, if you forgot to to do it beforehand, if you just add the line to the .gitignore file, all previously committed .pyc files will still be tracked, so you’ll need to remove them from the repository.

If you are on a Linux system (or “parents&sons” like a MacOSX), you can quickly do it with just this one line command that you need to execute from the root of the repository:

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

This just means:

starting from the directory i’m currently in, find all files whose name ends with extension .pyc, and pass file name to the command git rm -f

After *.pyc files deletion from git as tracked files, commit this change to the repository, and then you can finally add the *.pyc line to the .gitignore file.

(adapted from http://yuji.wordpress.com/2010/10/29/git-remove-all-pyc/)


回答 2

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

You have probably added them to the repository before putting *.pyc in .gitignore.
First remove them from the repository.


回答 3

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

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

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

echo "*.pyc" >> .gitignore

请享用。

i try to use the sentence of a prior post and don’t work recursively, then read some help and get this line:

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

p.d. is necessary to add *.pyc in .gitignore file to maintain git clean

echo "*.pyc" >> .gitignore

Enjoy.


回答 4

感谢@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存储库中删除文件。

Thanks @Enrico for the answer.

Note if you’re using virtualenv you will have several more .pyc files within the directory you’re currently in, which will be captured by his find command.

For example:

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

I suppose it’s harmless to remove all the files, but if you only want to remove the .pyc files in your main directory, then just do

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

This will remove just the app.pyc file from the git repository.