Completely remove files from Git repo and remote on GitHub

GitGit RemoteGit Filter-BranchGit Rm

Git Problem Overview


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 ./static/images folder.

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.

Git Solutions


Solution 1 - Git

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: https://stackoverflow.com/questions/872565/how-do-i-remove-sensitive-files-from-gits-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

Solution 2 - Git

You could just rebase your whole branch and remove both the commit that added the images and the commit that removed them.

git rebase -i master

You will be presented with a list of commits. Delete the lines that have the rogue commits you want to remove. ("dd" deletes a line in vim, the default editor. Then save with ZZ)

The dangling commits will then be cleaned up in the course of natural git garbage collection, a process you can force with the command given in Darhuuk's answer.

Edit: This will work even if you have pushed to a remote repository, but you'll have to push with --force. (The same applies to the git filter-branch solution).

Note that this will be very annoying to anyone who has pulled from your branch. They should consult "recovering from upstream rebase".


Presumably your original accidental addition of images is part of a commit you wish to keep. In this case, you need to edit the commit during the rebase to split out the parts you wish to keep. You can do this by replacing "pick" in the "rebase -i" list for that commit with "e" (for edit). The rebase process will stop here and you can split the commit with "git commit --amend".

Solution 3 - Git

I'm essentially repeating the answer combined with this Windows formatting caveat, only so it doesn't get lost as a comment.

Note the use of double-quotes rather than single quotes, because it depends on the shell that you're using how strings are parsed.

Single Line:

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

Multiple Lines:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbhijeet RastogiView Question on Stackoverflow
Solution 1 - GitAVHView Answer on Stackoverflow
Solution 2 - GitRJFalconerView Answer on Stackoverflow
Solution 3 - GitdrzausView Answer on Stackoverflow