If you work in consultancy, you might encounter a situation where you need to hand over a project repository to a competitor. However, the repository contains some proprietary code that shouldn't have been included in the first place. Is it possible to completely remove a directory from the git history? Let's explore how we can achieve this.
Prerequisites
The first and most important note is that I was unable to achieve this on Windows. Instead, I had to create a virtual machine with Linux (Ubuntu 22.04).
Git and python3 were preinstalled. Unfortunately, git alone won't suffice. You need to install 'git-filter-repo', a handy tool for rewriting git history. You can get it from GitHub (https://github.com/newren/git-filter-repo) or use pip to install it. I went with pip for simplicity.
First, install pip:
apt install python3-pip
Then install git-filter-repo:
python3 -m pip install --user git-filter-repo
Check if the installation was completed successfully:
git filter-repo --version
Now we have everything we need to accomplish our task.
Mirroring the Repo
Now we need to mirror the repository. Let's assume that the target repo already exists.
Create a bare clone of the repository:
git clone --bare https://github.com/EXAMPLE-USER/OLD-REPOSITORY.git
Mirror-push to the new repository:
cd OLD-REPOSITORY.git
git push --mirror https://github.com/EXAMPLE-USER/NEW-REPOSITORY.git
And clone the new repo:
cd ../NEW-REPOSITORY.git
git clone https://github.com/EXAMPLE-USER/NEW-REPOSITORY.git
Altering the History
Now let's put git-filter-repo to work.
git filter-repo --path <folder-path> --invert-paths
Replace <folder-path> with the actual path to the folder you want to remove. The --invert-paths option ensures that the specified folder is removed from all commits in the repository. git-filter-repo automatically performs garbage collection, so you don't need to clean up unnecessary files manually.
As a final step update the Remote Repository with the Modified History
git push --force --all
Don't forget the --all option to push all the branches at once.
Can We Do It Better?
There is always room for improvement. As you may have noticed, we had first pushed the repo to the third-party location and only then removed the history. You can surely achieve a better result by doing it in the proper order yourself.
Looking for an experienced Salesforce Architect?
- Are you considering Salesforce for your company but unsure of where to begin?
- Planning a Salesforce implementation and in need of seasoned expertise?
- Already using Salesforce but not fully satisfied with the outcome?
- Facing roadblocks in your Salesforce implementation and require assistance to progress?
Feel free to review my profile and reach out for tailored support to meet your needs!
Comments
Post a Comment