Abhijeet Rastogi Abhijeet Rastogi - 2 months ago 6x
Git Question

Completely remove files from Git repo and remote on GitHub

I accidentally added a folder of images and committed. Then, I made one more commit.
Then I removed those files using

git rm -f ./images
and committed again.

Right now, I have made a lot more commits in that branch (master). In my HEAD, I don't have that

Due to this, my repo size has increased a lot. How can I remove those blobs completely? And I want to remove it from my remote GitHub repo too.


This is what you're looking for: ignoring doesn't remove a file. I suggest you read that page, but here's the specific command to use:

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

Also, to remove all the deleted files from caches git creates, use:

rm -rf .git/refs/original/ && \
git reflog expire --all && \
git gc --aggressive --prune

You can find more info about the last command, as well as a script that does everything you want in one single action, here: git: forever remove files or folders from history.

Another links with lots of explanation: Remove sensitive data.

[Edit] Also, see this StackOverflow question: Remove sensitive files and their commits from Git history.

(Commands copied from natacado's answer in the question linked above.) If you have already removed the files from the working copy, the following should work. Find out the hash for the commit that added the unwanted files. Then do:

git filter-branch --index-filter \
'git update-index --remove filename' <introduction-revision-sha1>..HEAD
git push --force --verbose --dry-run
git push --force