【问题标题】:Remove large file from git commit [duplicate]从 git commit 中删除大文件 [重复]
【发布时间】:2019-09-20 12:44:45
【问题描述】:

我在我的项目源文件中添加了一个 150MB 的视频,并在不知不觉中提交了它。然后我尝试 git push 提交到我的远程仓库并注意到推送 hung 一段时间,然后最终失败,吐出这个:

Counting objects: 17, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (17/17), 145.48 MiB | 138.00 KiB/s, done.
Total 17 (delta 13), reused 0 (delta 0)
remote: Resolving deltas: 100% (13/13), completed with 12 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: 8b4191f1a1055e7dea4412f80b5125d2
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File src/images/greystone_place.mp4 is 145.45 MB; this exceeds GitHub's file size limit of 100.00 MB

我尝试将文件添加到.gitignore,但为时已晚。随后的git push 命令产生了相同的结果。

接下来,我尝试将 HEAD --soft 重置为我最初提交大文件的提交(这是在涉及.gitignore 的更多提交之后)。从那里,我使用git reset HEAD -- path/to/filename 将有问题的文件重置到工作目录并再次提交。这样做,我仍然遇到了同样的错误。

我后来意识到,也许使用--amend 命令可以解决问题(基于this article),但这也不起作用。

我还尝试在软重置时使用git rm --cached path/to/file。这也失败了。

我已经开始阅读有关 git rebase igit filter-branch 的信息,但它们看起来很复杂,我不确定我是否用这种方法找错了树。

我不是 Git 专家,但简而言之,我想对我的其他更改执行 git push 并排除这个大文件。不知何故,它似​​乎与较早的提交交织在一起,并阻止我向我的远程仓库创建 push

【问题讨论】:

    标签: git


    【解决方案1】:

    截至the tutorial on GitHub about removing file from history,您可以使用bfg tool 或使用您在问题中提到的git filter-branch

    git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all
    

    之后,您可能需要强制推送 (git push <b>-f</b>),因为历史记录已被修改。

    我不确定我用这种方法是不是找错了树。

    你可能先有一个备份 :-) GitHub 可能是另一个备份(因为你的推送失败)。

    【讨论】:

      【解决方案2】:

      您可以通过以下命令轻松完成,

      git reset --hard origin/<branch_name>
      

      你也可以用哈希在本地删除它。

      git reset --hard <SHA-Code>
      

      希望对你有帮助。

      【讨论】:

      • 这个命令会在我重置到的提交之后清除所有内容吗?我不想丢失我的其他更改。
      【解决方案3】:

      另一种更简单的方法:

      • git 再次克隆您的 GitHub 存储库,在一个新的单独文件夹中
      • 在那里报告您的修改(没有大文件)(您也可以从旧存储库中挑选过去的非推送提交)
      • 添加、提交和推送。

      【讨论】:

        猜你喜欢
        • 2014-02-05
        • 2020-09-14
        • 2011-05-10
        • 2012-03-06
        • 1970-01-01
        • 2013-03-22
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多