Removing a File from All past Git Commits
I have a tendency not to add
.DS_Store to my
.gitignore until I’ve already pushed to Github. Every single goddamn time it happens, I have to spend ten minutes googling for the answer.
Well, here be dragons:
git filter-branch --index-filter 'git rm --cached --ignore-unmatch .DS_Store' HEAD
filter-branchcommand rewrites commit history based on the filter that you provide.
--index-filtertells git to rewrite the index — it doesn’t check out the tree, which is apparently faster.
- The filter we’re passing tells git to remove the
.DS_Storefrom the staging area and delete it’s paths from the index — aka
ignore-unmatchflag does exactly that, the remove command exits with a 0 status even if no matches were found.
May the git lords have pity on your soul.