When you using Git, and accidentally commit the wrong files, you then feel a sudden panic. Keep calm! You can purging your history!
Do not be panic now. You can rewrite your history, but also you do not have to push your current local repo.
It has happened to me, when I try to add Git LFS and accidentally git add . There is a large binary files added to index.
Let’s Get Started Purging Your History at Git!
First, you can check for the files that you want to remove using this command:
git filter-branch --tree-filter 'rm files.exe'
What this command does to your local repo is, check for files.exe and try to check every commits and it’s taking so long. *if you have a large repo codebase
You might wanna consider to use this, instead:
git filter-branch --index-filter 'git rm -rn --cached --ignore-unmatch *.exe' --prune-empty -- --all
Due to checking only the already indexed files or commited files, you must delete it with git rm –cached files.exe commands. Otherwise, it will fails when checking your commit that didn’t have the files.
The command -rn means to check recursively inside directory and do a dry-run if you still unsure what were you doing XD
For —ignore-unmatch, to allow the commands continue running even the regex seems ugly XD
Things to note, I added –prune-empty is to remove your commit history that hasn’t not altered the files. *Due to the actual files has been removed and on the indexed files, it’ll has the result being an empty commits 🙂
Things to note, if you were on macOS. You might wanna check your .DS_Store files. It keep bothering me haha
You’ll see, git will do the rest when entering this powerful commands. You’ll experience tremendously long time to finish this commands. Or you might want to consider check this BFG Repo Cleaner, instead.
Last but not least, you’re good to go to push your changes without doubt now.
You can check for my terminal session here for further research.
Things to note, after you do this command. You might interested to see this article.
References (Advanced Topics):
That’s it, happy coding!!!
Mochamad Iqbal Dwi Cahyo is an Android Engineer at GITS Indonesia who loves creating and fixing bugs (a.k.a. features), also loves his family and wife. He wants to create the simplest way to solve a complex problem without repetitive and bloated code, so he chooses Kotlin as his weapon.