Rewriting git history with git filter-branch and git rebase -i

submited by
Style Pass
2021-06-09 10:00:09

Sometimes, you find yourself in a situation where you need to do rather drastic changes to a git repository, like when you actually want to remove one or more files including their full history from the repository. This is where git filter-branch can come in handy. In this blog post, we take a closer look at how it can help us with this task.

Warning: git filter-branch is a destructive git operation. When used unwisely, you can easily cause permanent damage to your repository, including removing files that you didn't intend to remove. Please inspect the results of the operation carefully. Do not force-push the result to your main repository without having one or more proper backups of the full repository, including the .git directory.

Here's what I had to begin with: a private project with some Ansible roles and associated playbooks. I wanted to make this public, to share it with the community. But, here's the catch: my project also contained my private inventory including both things like host_vars and also some files being deployed using these roles (more specifically, my the /etc/nginx/sites-enabled files for my nginx setup and other similar content).

The original structure was a simple git setup that worked well for me while working on configuring these machines. There were also some licensing issues that complicated the matter; some of the content was based on proprietary code written as part of my day-time job (at Hibox) that I couldn't just share with the world without sorting out the legal matters first.

Leave a Comment