Deleting files, completely, from git history
git, programming, typescript, git history, clean git history, delete files

FILENAME=""
git filter-branch --force --index-filter \\
"git rm --cached --ignore-unmatch $FILENAME" \\
--prune-empty --tag-name-filter cat -- --all
git push origin --force --all
git push origin --force --tags
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
Specify the name of the file you want to clean with
FILENAME="".With the
git filter-branchcommand, we can clean all traces of a specific file in Git history.The
forceparameter is used to prevent errors when the filter branch already exists.The
index-filterparameter specifies the command run for each commit.We remove all versions of the specified file with the
git rm --cached --ignore-unmatch $FILENAMEcommand.The
prune-emptyparameter removes empty commits that do not include file changes.The
tag-name-filter cat -- --allparameter performs operations on all branches and tags.We forcefully push the changes to the remote repository with the
git push origin --force --allandgit push origin --force --tagscommands.We delete the original references with the
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdincommand.We clean the git history with the
git reflog expire --expire=now --allandgit gc --prune=nowcommands.
Last updated
Was this helpful?