removing passwords from git repositories

04 Oct 2018

Here’s how to remove a password from any file, in all revisions, in a git repository:

$ git filter-branch --tree-filter \
    "find . -type f -exec sed -i -e 's/password/XXX/g' {} \;"

Another handy one, deleting all the lines containing word:

$ git filter-branch --tree-filter \
    "find . -type f -exec sed -i -e '/word/d' {} \;"

Finally, the classic remove file with sensitive data:

$ 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

Now, to force push your changes to a remote repository:

$ git push -f

That’s it, happy safe coding, 😊