How to remove big (>100MB) file from a GitHub repository and push successfully?

瘦欲@ 提交于 2019-11-28 11:51:35

Even though you have removed the file in the latest commit you still have a copy of it in your history. I think you're going to want to remove it from git completely.

You'll probably want to rebase it out. To find out when you introduced it you could do:

git log --reverse -n1 doc/image.eps

Then copy the SHA it gives you and do an interactive rebase:

git rebase -i sha~1

Keep the ~1 in the above command, but replace the sha with the actual SHA from the earlier command output. If the above command doesn't work you may need to set an EDITOR, e.g.:

EDITOR=vim git rebase -i sha~1

Replace vim with any command line editor you're comfortable with (emacs, nano, etc). You can get it to work with GUI editors like atom but you may need to pass in additional arguments to force the process to wait until you close the window. If you use atom you could run:

EDITOR="atom --wait" git rebase -i sha~1

This is going to take you way back in time. The very first line is going to have pick. You'll want to change that to an edit. Then save, and exit your editor. Do not change any other picks.

This will put you back at the commit that introduced the large file. You can now remove it from git:

git rm doc/image.eps && git commit --amend

Then continue the rebase:

git rebase --continue

If this goes all the way to completion, then you're done. You should be able to git push. However, if it doesn't, then you may have updated the image in a later commit. You'll want do the same git rm doc/image.eps && git commit --amend && git rebase --continue that we did above every time it stops.

I'm assuming quite a few things so I hope you're comfortable enough with git, editors, and the command line to use this information.

P.S. there is likely a much shorter and more succinct way to do this, but since you're asking this question I'm assuming you don't want a magical git command that will rip thru your history on its own. So first, let's try it step by step.

TrailDreaming

Here is how I got it to work after the "git push" got stuck due to adding and committing a big file and then continuing committing with other work while away from the internet:

I downloaded bfg*jar from:
https://rtyley.github.io/bfg-repo-cleaner/

cd tmpmirror; mkdir myrepo; cd myrepo; git clone --mirror ../../myrepo/.git
java -jar bfg-1.12.12.jar --strip-blobs-bigger-than 100M myrepo.git
cd myrepo.git; git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push https://github.com/traildreaming/myrepo
cd ../../..
mv myrepo myrepo_old
git clone https://github.com/traildreaming/myrepo
cd myrepo

If you get this message, then try with the extra steps from below

$ java -jar ../../bfg-1.12.13.jar --strip-blobs-bigger-than 100M myrepo.git

Using repo : [DIR]\tmpmirror\myrepo\myrepo.git

Scanning packfile for large blobs: 20681
Scanning packfile for large blobs completed in 135 ms.
Warning : no large blobs matching criteria found in packfiles - does the         repo need to be packed?
Please specify tasks for The BFG :
bfg 1.12.13
Usage: bfg [options] [<repo>]

  -b <size> | --strip-blobs-bigger-than <size>
        strip blobs bigger than X (eg '128K', '1M', etc)

```

cd tmpmirror; mkdir myrepo; cd myrepo; git clone --mirror ../../myrepo/.git
cd myrepo.git; git repack; cd ..
java -jar bfg-1.12.12.jar --strip-blobs-bigger-than 100M myrepo.git
cd myrepo.git; git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push https://github.com/traildreaming/myrepo
cd ../../..
mv myrepo myrepo_old
git clone https://github.com/traildreaming/myrepo
cd myrepo

And then continue working in the newly cloned repo. Thanks to the advice at Am I supposed to run BFG on the mirrored repo or the original? to use "git push https://github.com/traildreaming/myrepo" and not "git push".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!