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



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

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

我找到了几个解决方案:StackOverflow,这个博客,git和其他一些解决方案。他们似乎都在以同样的方式推动我:

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'

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

在我看来,我已经要求 git/Github 摆脱那些旧文件(通过终端命令)并完全忘记它们曾经存在过,所以他们将停止询问我关于它们的信息(通过 gitignore)。

我假设我做了一些乱序或错误的事情。您能给我的任何帮助将不胜感激。

.gitignore并没有

真正忽略文件

在 Git 中,当且仅当文件位于索引中时,才会跟踪该文件。1

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

一旦文件处于提交状态,它将永远处于该提交状态。 避免它的唯一方法是完全停止使用该提交。

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

将文件(或匹配的glob 模式,例如,使用***的任何内容)放入.gitignore中会告诉 Git:"不要抱怨,而且,如果它还没有在索引中,也不要自动添加它git add -A等。 但它不会将文件从索引中删除,并且实际上无法将文件从任何具有该文件的现有提交中删除。

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

您不仅在索引中拥有(或曾经拥有)文件(这意味着git add -A索引中更新它),而且在尚未推送的某些提交中也拥有该文件。 因此,将其从索引中删除是不够的。 您必须放弃包含大文件的每个提交。

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

另请参阅由于我已经删除的大文件而无法推送到 GitHub。


1索引是构建下一次提交的地方。 它本身不是提交,但是当你运行git commit时,Git 会打包索引内容以进行新的提交。

2工作树只是你处理文件的地方,因为 Git 索引和 Git 提交中的文件形式对于正常工作是不可用的。

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

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

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

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

将以下模式添加到 .gitignore

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

您首先必须将其删除,然后再次推动它。

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

最新更新