Skip to content

Instantly share code, notes, and snippets.

@RandyMcMillan
Forked from bartlomiejdanek/git-remove-file.sh
Created March 9, 2022 17:01
Show Gist options
  • Save RandyMcMillan/88ed3abf4771f870192b26ae29a2e71c to your computer and use it in GitHub Desktop.
Save RandyMcMillan/88ed3abf4771f870192b26ae29a2e71c to your computer and use it in GitHub Desktop.
remove file from git history
#!/bin/bash
set -o errexit
# Author: David Underhill
# Script to permanently delete files/folders from your git repository. To use
# it, cd to your repository's root and then run the script with a list of paths
# you want to delete, e.g., git-delete-history path1 path2
if [ $# -eq 0 ]; then
exit 0
fi
# make sure we're at the root of git repo
if [ ! -d .git ]; then
echo "Error: must run this script from the root of a git repository"
exit 1
fi
# remove all paths passed as arguments from the history of the repo
files=$@
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch $files" HEAD
# remove the temporary history git-filter-branch otherwise leaves behind for a long time
rm -rf .git/refs/original/ && git reflog expire --expire=now --all && git gc --prune=now && git gc --aggressive --prune=now
# after that git push origin branch_name --force
@RandyMcMillan
Copy link
Author

LINE: 21 use force with:
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch $files" HEAD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment