fbpx

Delete large files from historic commits in Git

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch <file>' HEAD

It can be tricky to remove a large file from git that goes several commits in the history, specifically when there are other source files in the same commits that you want to retain. The above command will do just this for you.

Caution: You will land up changing the hashes of all your commits that have the file in it. There maybe a massive conflict resolution that you may have to do after this command, but if you have to remove the file, this might just be the best way to get it done.

You are likely required to make this change, when Git is preventing pushes due to exceeding the file limit.

remote: error: File my_binary.tar.gz is 300.08 MB; this exceeds GitHub's file size limit of 100.00 MB

Use the git filter-branch command mentioned at top by replacing <file> with the location within the repository of the file you want to delete. You can also specify a folder if a complete folder is to be deleted.

Before you run the command make sure the <file> is currently deleted from the current branch. Also ensure that the delete of the file is committed within the repository even if the delete operation could not be pushed to remote.

Example Run

git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch my_binary.tar.gz' HEAD
Rewrite f1824be80ff0ff2bd27064094245288252fc479a (71/118) (3 seconds passed, remaining 1 predicted)    rm 'my_binary.tar.gz'
Rewrite dc4a60df97e753e98804cc432339a6b4675ba9e7 (71/118) (3 seconds passed, remaining 1 predicted)    rm 'my_binary.tar.gz'
Rewrite 09a87540b07fa5ba0c9dc0ba1861720f51cb5e98 (71/118) (3 seconds passed, remaining 1 predicted)    rm 'my_binary.tar.gz'
Rewrite e1be7aa558591bb06049808161fca0c97071165c (71/118) (3 seconds passed, remaining 1 predicted)    rm 'my_binary.tar.gz'
Rewrite 1925fa84c42e379f8dd1d44cbda18da52ac5091e (71/118) (3 seconds passed, remaining 1 predicted)    rm 'my_binary.tar.gz'
Rewrite b96b54fecbf7dec09274ef30990995bf4a97288a (71/118) (3 seconds passed, remaining 1 predicted)    rm 'my_binary.tar.gz'
Rewrite 75d364753f247d983e83ab408eaa0af2f2fff5a1 (71/118) (3 seconds passed, remaining 1 predicted)    rm 'my_binary.tar.gz'
Rewrite b839c3c1dd0e6277083b74801629e9138591be49 (95/118) (4 seconds passed, remaining 0 predicted)    
Ref 'refs/heads/master' was rewritten

Post running the command, attempt a push to remote. It will very likely not work. If it does not it is expected. Take a pull, resolve any CONFLICT that shows up, make sure the file you deleted is still deleted and if not delete it from your folder, commit the changes and push. Your push should now work.

%d bloggers like this: