How to delete a file from all commits?

Asked

Viewed 632 times

7

In my first commit I sent a binary file that has no reason to stay. And I sent a configuration file with real passwords. I changed the password to a fake one, deleted the binary, but if you go back to an old commit the data is still there.

I didn’t want to delete the repository and start a new one by losing my history of months worked to hide this little slip-up. But I really need these two files to be unrecoverable.

1 answer

8

You can use the filter-branch for this.

First, you need to track the commits that contain this file:

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch CAMINHO-PARA-SEU-ARQUIVO-COM-SENHAS' \
--prune-empty --tag-name-filter cat -- --all

Add the file to .gitignore (if you want to keep this password file only locally):

echo "SEU-ARQUIVO-COM-SENHAS" >> .gitignore
git add .gitignore
git commit -m "Add SEU-ARQUIVO-COM-SENHAS to .gitignore"

If it’s all right and your repository is available remotely (example, on Github), do a push to overwrite all changes in the repository:

git push origin --force --all

Do the same with tags, if you used any tag in the period the password file was available:

git push origin --force --tags

Recommend backup the repository before proceeding with the above tips.

  • 1

    Our seems I understood very well what he wanted not, +1!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.