When submitting code to github, it is possible to submit some private information to the repository. Even if the file is subsequently deleted, the content of the file can still be found in github's commit history.

Therefore, if data needs to be deleted from history, you can use the git filter-branch command or the BFG Repo-Cleaner open source tool.

This article mainly introduces how to use the git filter-branch command to clear the content of specified files in history.

It is worth noting that if we clear the records in history, we will subsequently be unable to use other commands to view the commit records and changes of that file.

Let's start using git filter-branch to clear file commit records.

  1. Enter the project root path
cd YOUR-REPOSITORY
  1. Execute git filter-branch

Note that after executing this command, the corresponding file will also be deleted. If you do not want the file to be deleted, you can back it up first and restore it later.

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' \
--prune-empty --tag-name-filter cat -- --all

Replace PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA with the path of the file relative to the project.

  1. Add the file to .gitignore to prevent the file from being submitted again.
echo "YOUR-FILE-WITH-SENSITIVE-DATA" >> .gitignore
git add .gitignore
git commit -m "Add YOUR-FILE-WITH-SENSITIVE-DATA to .gitignore"
  1. If you are sure that push will not cause conflicts, you can use
git push origin --force --all

to update all your branches.

  1. If you need to delete sensitive information from tagged versions, you can use
git push origin --force --tags

At this point, the sensitive file can be removed.

Official Guide