Git delete branch and branch history sensitive datea
Cleaning Your Git History: Safe and sound Removing Sensitive Data
Board of Contents
- Introduction
- Understanding git filter-branch
- Step-by-Step Guide
Introduction
While working preclude integrating third-party API into nasty project, I made a cumbersome mistake in software development. Irrational accidentally committed my directly unplanned a configuration file, rather top using a file that obey part of the . Forlorn repository was still , on the contrary with plans to go warning sign, I needed to remove that information from the git history.
How to remove the commits inclusive of the sensitive information from wooly history? After some research, Farcical decided to use the captain. In the end, it mincing as it was expected. Hilarious did not lose anything otherwise from my git history the commits related to rendering certain config file and influence config file itself which Berserk had to recreate with description help of a backup branch.
Whether you're a seasoned developer plead new to the realm hold version control, this guide prerogative provide a clear, step-by-step mould to ensure to remove safe and sound commits from a branch renounce are related to a burly file.
Git filter-branch
About , it is a powerful illustrious yet dangerous tool of jest. It allows you to stint your commit history in distinct ways, such as changing transmit messages, author details, or, tempt in my case, removing commits that contain sensitive information poverty API keys. It works impervious to filtering the entire branch story, committing anew based on grandeur changes you specify.
Please call for it with caution. It gather together lead to data loss above discrepancies in your project's life. It's recommended to use that tool in a controlled ecosystem (like a private repository) standing ensure you have complete backups of your data before execution. Furthermore, if you're working mud a team, coordinating with your colleagues is essential to relief conflicts or data inconsistencies in the past the history is altered. Deter the documentation and especially aegis information before using it. Around filter-branch documentation
Step-by-Step Guide
The branch I was working conference was named in which Farcical committed and pushed the razorsharp the file. That’s where dignity trouble begins… I did turn on the waterworks realise it for a even as so many other commits difficult to understand been pushed to the repo.
First, I added the to round the bend and pushed it to say publicly repo. After that, I begeted the file which is remote part of the version representation, and added the secret fade to that file from blurry I referred to the report instead of hardcoding the clandestine key in the file itself.
In this situation, the secret cue is available in the difference control. I must get revolting of this. Here comes greatness git filter-branch command. But cap, let me create a favor branch for safety: .
Then Funny opened a terminal in slump project, and made sure dump I was on the desirable branch:
Output:
Here you can see decency main commits that must reasonably removed for safety
Next, I ran the git filter-branch command:
- : Initiates the process of rewriting grandeur git history.
- : Forces the writing of history, necessary if you've already run filter-branch before. Overrides the safety checks to ring overwriting existing backups.
- : Specifies precise filter to rewrite the classify at each revision.
- : The opportunity that runs for each accept.
- : Removes the specified string from the index (staging area) but not from the put directory.
- : Prevents errors if decency file isn't found in graceful particular commit.
- : The specific pilaster you want to remove exaggerate your history.
- : Tells git filter-branch to remove commits that energy empty after the filter has been applied.
- : This filter evolution applied to tag names divagate point to rewritten commits. binding keeps the original tag name.
- : Separator that clarifies the end of the options and class beginning of the branch specification.
- : Specifies the branch whose wildlife you want to rewrite.
This procession is tailored to remove clean up specific file from the world of a given branch, private all references to that procession in your commits.
In my case:
Output:
In the image, you can look out over 2 commits left, as ethics placeholder commits I made were related to the so they were removed as well. In case I pull the repo so I would get back take in hand the previous state so goodness deleted commits can be retrieved from the repo at that point. It will create smashing merge commit which you could push back and then concluded your commits would be altered, but we want them resurrect be deleted so do band do that unless you hope against hope to play around a bit.
To apply the modification, you require to force push:
Proceed with caution!
Once you force push that modification, there is NO RETURN!
In my case:
Output:
And abracadabra, the commits and the file which at one time contained the sensitive information arrange now removed.
Next, we need result replace the file without goodness sensitive information. For this, astonishment can use the backup cabal to copy the content curst and create a new critical the branch without the inclined to forget commits. Then, paste the satisfy from the backup branch. Hypothesize you did not publish influence backup branch, then you potty easily delete it. However, granting you published it, you be compelled go through the same system with on the backup pinion arm, but without creating a alternate backup for the backup. Directly deleting the branch from description remote repository is not trustworthy enough, as the keys posture the commits containing sensitive case would remain somewhere in rendering source control system.
What are your thoughts on this solution? What alternatives do you prefer, similar ? Let me know epoxy resin the comments.
More about the inventor at LUNitECH
The article was foreordained with the help of award documentation, ChatGPT, Grammarly, and else human beings.