Developer Stories Git — Oh, No, You Wrong Commit! Then, Purging Your History?

Git — Oh, No, You Wrong Commit! Then, Purging Your History?

When you using Git, and accidentally commit the wrong files, you then feel a sudden panic. Keep calm! You can purging your history!

Be cool.

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.

According to GitHub quota and when working with large files, you cannot push a 100MB files into GitHub remote repo. Unless, you use GitLab instead, due to its support large binary files recently.

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.

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.


Please enter your comment!
Please enter your name here

More articles

Latest article

GITS Webinar: Adopting DevOps as Culture

GITS Webinar hadir kembali! Kali ini, kita akan membahas tentang DevOps. DevOps bisa dibilang sebagai culture suatu company dalam...

Sales dan Marketing Budgeting untuk 2021

Dalam menjalankan bisnis perusahaan, membuat sales budgeting dan marketing budgeting adalah hal penting. Ini untuk merencanakan dan mengelola kinerja keuangan dengan ketat....

Menciptakan Organisasi Pembelajaran/Learning Organization, juga bagi Digital Agency Jakarta

Dalam sebuah perusahaan, juga digital agency Jakarta misalnya, kedinamisan adalah hal yang mutlak. Setiap orang yang ada di dalamnya memiliki perbedaan latar...

Software Developer Indonesia, Ini Cara Pakai Open XML di .NET Core

Bagi software developer Indonesia, saat ini, banyak aplikasi yang menerapkan office tools untuk membantu pengerjaan dokumen, presentasi, maupun spreadsheet. Office tools ini...

Cara Membuat UI/UX Design Portfolio yang Menarik Perhatian Recruiter

Bidang UI/UX design terus berkembangan pesat dan memiliki banyak peluang. Dengan memiliki design portfolio yang kuat dan menarik, kamu punya banyak kesempatan...