当前位置:   article > 正文

Git ---- push时遇到大文件如何解决?_git push 大文件

git push 大文件

在做代码开源的时候,发现github有文件size的限制,超过100MB的大文件无法推送成功。

如何真正移除git文件且不影响版本库:

方法:使用 Rebase 或 filter-branch 指令來整理

1、git filter-branch 

  1. $ git filter-branch --tree-filter "rm -f config/test.c"
  2. Rewrite 27f6ed6da50dbee5adbb68102266a91dc097ad3f (7/7) (0 seconds passed, remaining 0 predicted)
  3. Ref 'refs/heads/master' was rewritten

个人更使用喜欢参数为--index-filter或者--all filtered all refs;

$ git filter-branch -f --index-filter "git rm -f --cached --ignore-unmatch -fr config/test.c" A..B

参数--tag-name-filter cat 可以删除原始tag

2、这里的删除并不彻底,随时可以恢复文件,仅仅是回收内存的操作。我们需要清理所有散落的object和并删除git的备份 ,使其完全失效。

目录.git/refs/original下,还有相关备份:

$ rm .git/refs/original/refs/heads/master # $ rm .git/refs/original 删除git的备份

3、清理所有散落的object:

  1. $ git reset refs/original/refs/heads/master --hard
  2. or
  3. $ git reflog&&git checkout $commit-SHA1

git reflog expire --all --expire=now

4、再次验证是否有散落的object, 验证数据库中对象的连接性和有效性,所以继续执行:

  1. $ git fsck --unreachable

5、git的垃圾清理车最终删除那些对象  git gc --aggressive --prune=now , --aggressive 此选项将导致git gc更积极地优化存储库,但代价是花费更多时间。

--prune=<date> Prune loose objects older than date 修剪比日期更早的松散物体。

可以在此之前执行 git repack -A -d 在存储库中打包解压缩的对象。

删除冗余的对象用$ git fsck #检查是否还有对应的object,现在已经找不到提交的sha1了。

$ git gc --prune=now 

可以使用该脚本查看大文件(https://github.com/appke/geekBigData/blob/master/findBigFile.sh)

  1. #!/bin/bash
  2. #set -x
  3. # Shows you the largest objects in your repo's pack file.
  4. # Written for osx.
  5. #
  6. # @see https://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
  7. # @author Antony Stubbs
  8. # set the internal field spereator to line break, so that we can iterate easily over the verify-pack output
  9. IFS=$'\n';
  10. # list all objects including their size, sort by size, take top 10 查出已删除的大文件前10名的commit SHA值
  11. objects=`git verify-pack -v .git/objects/pack/pack-*.idx | grep -v chain | sort -k3nr | head`<br>
  12. echo "All sizes are in kB's. The pack column is the size of the object, compressed, inside the pack file."
  13. output="size,pack,SHA,location"
  14. for y in $objects
  15. do
  16. # extract the size in bytes
  17. size=$((`echo $y | cut -f 5 -d ' '`/1024))
  18. # extract the compressed size in bytes
  19. compressedSize=$((`echo $y | cut -f 6 -d ' '`/1024))
  20. # extract the SHA
  21. sha=`echo $y | cut -f 1 -d ' '`
  22. # find the objects location in the repository tree
  23. other=`git rev-list --all --objects | grep $sha`
  24. #lineBreak=`echo -e "\n"`
  25. output="${output}\n${size},${compressedSize},${other}"
  26. done
  27. echo -e $output | column -t -s ', '
  • 查看文件大小
du -ah .git/objects
  • 占用空间最多的五个文件 链接:https://www.jianshu.com/p/fe3023bdc825
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"

git filter-branch -- 重写 branches: https://cloud.tencent.com/developer/section/1138641

  1. git filter-branch [--env-filter <command>][--tree-filter <command>][--index-filter <command>][--parent-filter <command>]<br>[--msg-filter <command>][--commit-filter <command>][--tag-name-filter <command>][--subdirectory-filter <directory>]<br>[--prune-empty][--original <namespace>][-d <directory>][-f|--force][--][<rev-list options>…​]
  2. --env-filter <command> #修改将在其中执行提交的环境,常用于重写作者/提交者名称/电子邮件/时间环境变量。\<br>(GIT_AUTHOR_NAME,GIT_AUTHOR_EMAIL,GIT_AUTHOR_DATE,GIT_COMMITTER_NAME,GIT_COMMITTER_EMAIL,GIT_COMMITTER_DATE) e.g.
  3. git filter-branch --env-filter '
  4. if test "$GIT_AUTHOR_EMAIL" = "root@localhost"
  5. then
  6. GIT_AUTHOR_EMAIL=john@example.com
  7. export GIT_AUTHOR_EMAIL
  8. fi
  9. if test "$GIT_COMMITTER_EMAIL" = "root@localhost"
  10. then
  11. GIT_COMMITTER_EMAIL=john@example.com
  12. export GIT_COMMITTER_EMAIL
  13. fi
  14. ' -- --all
  15. --tree-filter <command> #用于重写tree及其内容的过滤器。新增或删除文档
  16. --index-filter <command> #重写索引的过滤器。类似于树过滤器,但不检出树,因此速度更快。经常与结合使用git rm --cached --ignore-unmatch ...<br><br>##--tree-filter将每个提交签出到临时目录中,运行filter命令,并从临时目录中的任何内容生成新的提交;<br>##而--index-filter将每个提交复制到索引中,运行filter命令,并从索引中的任何内容生成新的提交。
  17. --parent-filter <command> #用于重写提交的父列表的过滤器。它将在stdin上接收父字符串,并在stdout上输出新的父字符串。
  18. --msg-filter <command> #重写提交消息。
  19. --commit-filter <command> #执行提交的过滤器
  20. --tag-name-filter <command> #重写标签名称,原始标签不会被删除,但可以被覆盖;使用“ --tag-name-filter cat”来简单地更新标签。\<br>在这种情况下,请格外小心,并确保备份了旧标签,以防转换失败。
  21. --subdirectory-filter <directory> #只查看涉及给定子目录的历史记录
  22. --prune-empty #过滤常常生成空的提交,从而使树保持不变。此参会删除空提交。因此仅适用于只有一个父对象的提交它将保留合并点。与--commit-filter不兼容。
  23. --original <namespace> #使用此选项设置原始提交将存储在其中的名称空间。默认值是refs/original。
  24. -d <directory> #使用此选项可将路径设置为用于重写的临时目录。使用tree-filter, 该命令需要暂时将该树检出到某个目录,大型项目的情况下可能消耗很大空间。<br>默认情况下,它在.git-rewrite/目录中执行此操作,但您可以通过此参数覆盖该选项。
  25. -f/--force #git filter-branch拒绝从现有的临时目录开始过滤,或者当已经有ref时refs/original/,使用该参数强制执行过滤。
  26. <rev-list options>…​ #参数git rev-list。<br>#所有positive refs都被重写。您也可以指定选项--all,但您必须使用--将修订选项与git filter-branch选项分开。--all重写所有分支和标记。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/481508
推荐阅读
相关标签
  

闽ICP备14008679号