Skip to main content

Very useful for removing unwanted files in a git repository. Cloning a 1GB repository because someone accidentally committed in a video.

# How To Completely Remove a File From Git History
#
# by: Marius Ducea
# http://www.ducea.com/2012/02/07/howto-completely-remove-a-file-from-git-history/

# First we need to identify the file that is causing this issue; and for
# this we will verify all the packed objects and look for the biggest ones:

git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5

# (and grab the revisions with the biggest files). Then find the name of the
# files in those revisions:

git rev-list --objects --all | grep <revision_id>

# Next, remove the file from all revisions:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch <filename>'
rm -rf .git/refs/original/

# Edit ".git/packed-refs" and remove/comment any external pack-refs.
# Without this the cleanup might not work. I my case I had
#  "refs/remotes/origin/master" and some others branches.

vim .git/packed-refs

# Finally repack and cleanup and remove those objects:

git reflog expire --all --expire-unreachable=0
git repack -A -d
git prune

# Hopefully these steps will help you completely remove those un-wanted
# files from your git history. Let me know if you have any problems after
# following these simple steps.

# Note: if you want to test these steps here is how to
# quickly create a "test" repo:

# Make a small repo
mkdir test
cd test
git init
echo hi > there
git add there
git commit -m 'Small repo'

# Add a random 10M binary file
dd if=/dev/urandom of=testme.txt count=10 bs=1M
git add testme.txt
git commit -m 'Add big binary file'

# Remove the 10M binary file
git rm testme.txt
git commit -m 'Remove big binary file'