diff --git a/snippets/purge-file.md b/snippets/purge-file.md new file mode 100644 index 000000000..a8e4fd124 --- /dev/null +++ b/snippets/purge-file.md @@ -0,0 +1,27 @@ +--- +title: Purge a file from history +tags: repository,remote,advanced +--- + +Completely purges a file from history. + +- Use `git rm --cached --ignore-unmatch ` to delete the file in the specified ``. +- Use `git filter-branch --force --index-filter --prune-empty --tag-name-filter cat -- --all` to rewrite the branch's history, passing it the previous command. +- You can optionally use `git push --force -all` to force push the changes to the remote repository. +- ⚠️ **WARNING**: This is a destructive action that rewrites the history of the entire repository. Make sure you know what you are doing. + +```sh +git filter-branch --force --index-filter \ + "git rm --cached --ignore-unmatch " \ + --prune-empty --tag-name-filter cat -- --all +git push --force --all +``` + +```sh +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 +```