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
- git undo deleted files
- How to ignore files which are in repository?
- Completely remove file from all Git repository commit history
- When is git rm -f used?
- git rm -r doesn't work in a submodule
- How to safely remove files from master which have already been removed from a branch?
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.
3 Solutions collect form web for “Completely remove files from Git repo and remote on GitHub”
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
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”.
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.
git filter-branch --index-filter "git rm -r --cached --ignore-unmatch <file/dir>" HEAD
git filter-branch --index-filter \ "git rm -r --cached --ignore-unmatch <file/dir>" HEAD