为什么我在 gitignore 中列出的文件会收到来自 Github 的大文件警告?

Why do I get an Large File Warning from Github for a file that i have listed in gitignore?

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

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

我找到了几个解决方案:Whosebug, this blog, git 和其他几个。他们似乎都在以同样的方式推动我:

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

我已经 运行 此代码的一些变体,例如删除 **file/**--cachedimage/file/**,但它没有' 改变我仍然可以在 GitHub 分支上看到文件的事实。

此外,我已将此添加到我的 git忽略文件中:/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'

所以,现在我很困惑,因为在我的 gitignore 文件中添加 /public/uploads/image/file/** 不是告诉 git 忽略文件夹和其中的文件吗?为什么文件一直被推送到我的仓库?

在我看来,我已经要求 git/Github 删除那些旧文件(通过终端命令)并完全忘记它们曾经存在过这样他们就不会再问我关于它们的问题(通过 git忽略)。

我假设我做的事情不按顺序或不正确。如果您能给我任何帮助,我们将不胜感激。

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

您已将文件添加到 .git忽略 它已添加到 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 ....

.gitignore 并没有真正忽略文件

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

如果一个文件在索引中并且您进行了新的提交,该文件将进入提交。无论文件名是否在 .gitignore.

中,都会发生这种情况

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

.gitignore 所做的是让 Git 停止抱怨。对于 work-tree、2 但不在索引中的每个文件,Git 抱怨: "hey, this file is in the work-tree but not in the index! Maybe you should add it!" 但有些文件确实属于work-tree 不属于任何提交,因此永远不应进入索引。

将文件或匹配的 glob 模式,例如任何使用 *** 的内容放入 .gitignore 告诉 Git: "Don't complain, and also, if it's not already in the index, don't automatically add it either with git add -A etc." 但它不会将索引的文件 out 取出来,字面意思是 can't从任何现有的提交文件中删除它。

要从索引中删除一个文件,而不是从 work-tree 中删除它,请使用 git rm --cached3

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

为此,您可能希望使用 git rebase -i 复制 提交(或那些提交)到新的和改进的版本,其中改进只是"do not include the file in the commit".

另见 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 都会扩展 **,而那些扩展的也不总是以相同的方式进行。其次,work-tree 内容可能与索引内容有很大不同:例如,如果 work-tree 中有 public/uploads/image/file/1 而索引中没有,则 shell,它查看 work-tree,可能会在其 glob 扩展中包含它,而 Git,它在执行 Git 时只查看 Git 的索引=24=],不会将其放入要删除的文件列表中——一旦 Git 找到 one 无法从索引中删除的文件,它就会停止删除 其他 个文件。