Skip to content

Remove a File Permanently from Git Repository

Here is a method to remove a file from a Git repository permanently. It deletes the file from all commits (history).

# Syntax:
#  git filter-branch --force --index-filter \
#    "git rm --cached --ignore-unmatch <path>" \
#    --prune-empty --tag-name-filter cat -- --all
#  git push <remote> --force --all

# Replace "config/apiKeys.json" with the path to the file you want deleted.
git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch config/apiKeys.json" \
  --prune-empty --tag-name-filter cat -- --all
# Purges `config/apiKeys.json` from history

git push origin --force --all
# Force pushes the changes to the remote repository

References

Purge a file from Git history