【问题标题】:Why do I get an Large File Warning from Github for a file that i have listed in gitignore?为什么我在 gitignore 中列出的文件会从 Github 收到大文件警告?
【发布时间】:2017-06-04 11:12:36
【问题描述】:

没有真正考虑,我一直在提交我在开发中使用的图像,然后将其推送到我的 Github 存储库。

在发现这导致我无法将项目推送到我的分支后,我搜索了一种解决方案来从我的存储库中删除这些图像,然后将这些图像添加到我的 gitignore 文件中。

我找到了几个解决方案:StackOverflowthis bloggit 和其他一些解决方案。他们似乎都在以同样的方式推动我:

git rm --cached -r /public/uploads/image/file/** 

我已经运行了该代码的一些变体,例如删除 **file/**--cachedimage/file/**,但这并没有改变我仍然可以在我的GitHub 分支。

我也将此添加到我的 gitignore 文件中:/public/uploads/image/file/**

但是当我推送到存储库分支时,我得到了这个信息,告诉我为什么我不能推送到 Github:

我从git add . 开始了解上下文。

ruby 2.3.3-p222
╳  project_name categories ◆ git add .                                                               

ruby 2.3.3-p222
╳  project_name categories ◆ git commit -m "trying to get a commit in after purging development environment image data"
[categories 8c13b0a] trying to get a commit in after purging development environment image data
 1 file changed, 1 insertion(+), 3 deletions(-)

ruby 2.3.3-p222
╳  project_name categories  git push origin categories                                               
Counting objects: 3840, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3664/3664), done.
Writing objects: 100% (3672/3672), 163.83 MiB | 3.98 MiB/s, done.
Total 3672 (delta 1242), reused 0 (delta 0)
remote: Resolving deltas: 100% (1242/1242), completed with 57 local objects.
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
remote: error: Trace: 85ba931580b369a222fcf5903416f84e
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File public/uploads/image/file/30/show_55MiEk4_-_Imgur.gif is 119.49 MB; this exceeds GitHub's file size limit of 100.00 MB
To git@github.com:Lenocam/project_name.git
 ! [remote rejected] categories -> categories (pre-receive hook declined)
error: failed to push some refs to 'git@github.com:Lenocam/project_name.git'

所以,现在我很困惑,因为没有将/public/uploads/image/file/** 添加到我的 gitignore 文件中告诉 git 忽略文件夹和其中的文件?为什么文件会继续推送到我的存储库?

在我看来,我已经要求 git/Github 删除那些旧文件(通过终端命令)并完全忘记它们曾经存在过,因此他们将不再向我询问它们(通过 gitignore)。

我假设我做错了事或做错了事。您能给我的任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails git github gitignore delete-file


    【解决方案1】:

    .gitignore 并没有真正忽略文件

    在 Git 中,跟踪文件当且仅当它在索引中。1

    如果一个文件在索引中并且您进行了新的提交,则该文件将进入该提交。无论文件名是否在 .gitignore 中,都会发生这种情况。

    一旦文件处于提交中,它就会在该提交中永远。避免它的唯一方法是完全停止使用该提交。

    .gitignore 所做的是让 Git 停止抱怨。对于你在工作树中的每个文件,2 但不在索引中,Git 抱怨:“嘿,这个文件在工作树中但不在索引中!也许你应该添加它!”但是一些确实属于工作树的文件不属于任何提交,因此不应该进入索引。

    将文件(或匹配的 glob 模式,例如使用 *** 的任何内容)放入 .gitignore 会告诉 Git:“不要抱怨,而且,如果它不在索引中,也不要使用git add -A 等自动添加它。"但它不会从索引中取出文件 ,而且它确实不能从任何现有的提交中取出文件。

    要从索引中删除文件,而不是从工作树中删除,请使用git rm --cached3

    您不仅在索引中拥有(或拥有)该文件——这意味着git add -A 在索引中更新它——你还在一些尚未推送的提交中拥有它。所以从索引中删除它是不够的。您必须放弃每个包含大文件的提交。

    为此,您可能希望使用git rebase -i 将提交(或那些提交)复制到一个新的和改进的版本,其中改进只是“不要将文件包含在提交”。

    另见Can't push to GitHub because of large file which I already deleted


    1index 是您构建 next 提交的位置。它本身不是一个提交,但是当你运行git commit 时,Git 会打包索引内容以进行新的提交。

    2work-tree 只是您处理文件的地方,因为 Git 索引和 Git 提交中的文件形式无法用于正常工作。

    3请注意,不应shell 扩展您在 .gitignore 文件中使用的任何 glob 模式,原因有两个.首先,shell 扩展可能与 Git 所做的不匹配。具体来说,并不是所有的 shell 都扩展 **,而那些扩展的 shell 并不总是以相同的方式进行。其次,工作树内容可能与索引内容有很大不同:例如,如果工作树中有 public/uploads/image/file/1 而索引中没有,则 shell 会查看工作树,可能会在它的全局扩展中包含它,而在执行git rm 时只查看 Git 的索引的 Git 不会将它放在要删除的文件列表中 - 一旦 Git 找到 one 文件无法从索引中删除,它停止删除 其他 文件。

    【讨论】:

    • 感谢您的深入解释。我点击链接并运行此命令git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch public/uploads/image/file/30/show_55MiEk4_-_Imgur.gif' HEAD 它已从提交中删除文件,我推送到存储库并收到此错误:remote: error: File 2b0baafa8f4c5dc203a53ec92f0db5730284516f 这是同样的问题,但没有路径。我手动搜索但仍然不知道这个文件来自哪里。我是否应该再次运行相同的命令,但将文件路径替换为2b0baafa8f4c5dc203a53ec92f0db5730284516f
    • 这有点奇怪,如果它只是说remote: error: File <hash>。它来自另一个 Git(大概在 GitHub 上),但通常不是他们报告问题的方式。您需要联系他们以了解他们报告的原因。
    【解决方案2】:

    git rm --cached -r /public/uploads/image/file/**

    您已将文件添加到 .gitignore 它已经添加到 git 之后。 看起来您的忽略模式与文件模式不匹配

    public/uploads/image/file/30/show_55MiEk4_-_Imgur.gif 
    

    将以下模式添加到 .gitignore

    /public/uploads/image/file/**/**
    

    您必须先将其移除,然后再将其推入。

    git rm --cached <file>
    git commit -m "Message"
    
    git push ....
    

    【讨论】:

    • 不应该/file/** 删除文件夹file 中的所有内容,包括文件夹30 及其内容?还是我误解了**
    • 来自我的终端:git rm --cached -r public/uploads/image/file/**/** fatal: pathspec 'public/uploads/image/file/1' did not match any files 一方面它说,这里没有文件,另一方面,当我尝试推送它时,它说我正在尝试的文件之一忽略太大。
    猜你喜欢
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多