Delete sensitive string from all Git commits

75 Views Asked by At

I have internal github in our organization. There is a particular git repo where I need to replace a sensitive string from all commits. There are around 2000 commits and the sensitive string is there from first commits. There are 7 valid branches as well. How can I delete/replace the sensitive string from all commits. I dont have internet access for the machine nor I have privileges to download bfg repo cleaner tool. I am trying my luck with git filter-branch command below. But this has not helped yet.

git filter-branch -f --msg-filter 'sed "s/sensitivedata/noman/g"' -- --all

If nothing works my last resort is to delete the repo and create new one. But before doing that I wanted to see if I have an option preserving existing commits. Any help and light towards this is highly appreciated.

1

There are 1 best solutions below

2
ankur yadav On

I was facing the similar problem. Try using below script, it worked for me.

# Replace "sensitivedata" with "noman" in all commits across specified branches
branches=("branch1" "branch2" "branch3")  # Add all valid branch names
for branch in "${branches[@]}"; do
    git checkout "$branch"
    git filter-branch -f --tree-filter 'find . -type f -exec sed -i "s/sensitivedata/noman/g" {} +' -- --all
done