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-branch command, we can clean all traces of a specific file in Git history.

  • The force parameter is used to prevent errors when the filter branch already exists.

  • The index-filter parameter specifies the command run for each commit.

  • We remove all versions of the specified file with the git rm --cached --ignore-unmatch $FILENAME command.

  • The prune-empty parameter removes empty commits that do not include file changes.

  • The tag-name-filter cat -- --all parameter performs operations on all branches and tags.

  • We forcefully push the changes to the remote repository with the git push origin --force --all and git push origin --force --tags commands.

  • We delete the original references with the git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin command.

  • We clean the git history with the git reflog expire --expire=now --all and git gc --prune=now commands.

Last updated

© 2024 ~ Yunus Emre Ak ~ yEmreAk