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

enf*_*fix 84 python git

如何忽略.pycgit中的文件?

如果我把它放在.gitignore它不起作用.我需要它们不被跟踪而不检查提交.

Enr*_* M. 199

你应该添加一行:

*.pyc 
Run Code Online (Sandbox Code Playgroud)

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

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

如果您使用的是Linux系统(或像MacOSX这样的"父母和儿子"),只需使用您需要从存储库的根目录执行的这一行命令即可快速执行此操作:

find . -name "*.pyc" -exec git rm -f "{}" \;
Run Code Online (Sandbox Code Playgroud)

这只是意味着:

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

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

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

  • 另外,只要从git和本地计算机中删除文件,就可以从顶层目录执行git rm --cached * .pyc。从[这里](https://coderwall.com/p/wrxwog/why-not-to-commit-pyc-files-into-git-and-how-to-to-fix-if-you-readydid中得到它) (2认同)

ral*_*nja 77

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

  • 每次都有这个问题 (36认同)

Ign*_*ams 37

把它放进去.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".
Run Code Online (Sandbox Code Playgroud)

因此,要么指定相应*.pyc条目的完整路径,要么将其放在.gitignore从存储库根目录(包括)引出的任何目录中的文件中.

  • 为了避免让其他人感到困惑,Ignacio对手册页的解释是错误的.您不需要将*.pyc放在同一目录中,只需将其放在父目录(或祖父母等)中即可. (5认同)

Dar*_*lez 10

我尝试使用前一篇文章的句子并且不递归工作,然后阅读一些帮助并得到这一行:

find . -name "*.pyc" -exec git rm -f "{}" \;
Run Code Online (Sandbox Code Playgroud)

pd需要在.gitignore文件中添加*.pyc以保持git干净

echo "*.pyc" >> .gitignore
Run Code Online (Sandbox Code Playgroud)

享受。


小智 6

如果您想全局忽略“.pyc”文件(即,如果您不想在每个 git 目录的 .gitignore 文件中添加该行),请尝试以下操作:

$ cat ~/.gitconfig 
[core]
    excludesFile = ~/.gitignore
$ cat ~/.gitignore
**/*.pyc
Run Code Online (Sandbox Code Playgroud)

[参考]
https://git-scm.com/docs/gitignore

  • 用户希望 Git 在所有情况下忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常会进入用户的 ~/.gitconfig 中由 core.excludesFile 指定的文件。

  • 前导“**”后跟斜杠表示在所有目录中匹配。例如,“**/foo”匹配任意位置的文件或目录“foo”,与模式“foo”相同。"**/foo/bar" 匹配直接位于目录 "foo" 下的任何位置的文件或目录 "bar"。