How to Remove a Large File from Commit History in Git?

Step 1: Check the Size of Git History

du -hs .git/objects

Step 2: Create a script to find larges files

vim git_find_big.sh#!/bin/bash
#set -x
# Shows you the largest objects in your repo's pack file.
# Written for osx.
#
# @see http://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
# @author Antony Stubbs
# set the internal field spereator to line break, so that we can iterate easily over the verify-pack output
IFS=$'\n';
# list all objects including their size, sort by size, take top 10
objects=`git verify-pack -v .git/objects/pack/pack-*.idx | grep -v chain | sort -k3nr | head`
echo "All sizes are in kB's. The pack column is the size of the object, compressed, inside the pack file."output="size,pack,SHA,location"
for y in $objects
do
# extract the size in bytes
size=$((`echo $y | cut -f 5 -d ' '`/1024))
# extract the compressed size in bytes
compressedSize=$((`echo $y | cut -f 6 -d ' '`/1024))
# extract the SHA
sha=`echo $y | cut -f 1 -d ' '`
# find the objects location in the repository tree
other=`git rev-list --all --objects | grep $sha`
#lineBreak=`echo -e "\n"`
output="${output}\n${size},${compressedSize},${other}"
done
echo -e $output | column -t -s ', '

Step 3: Find Top 10 Large files

./git_find_big.sh

Step 4. Install git-filter-repo

pip3 install git-filter-repo

Step 5: Remove large files from the .git history

git filter-repo --force --path <large file path> --invert-paths

Step 6: Clean Unused Fils

git reflog expire --expire=now --allgit gc --prune=now

Step 7. Add Git Remote URL and Push Back the Result

# Add Your Github URL
git remote add origin <Your Github URL>
# Push the result to all the branches
git push --all --force
# Push the result to origin repo
git push -u origin --all
git push -u origin --tags -f

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store