Skip to content

Instantly share code, notes, and snippets.

@k4gdw
Forked from bartlomiejdanek/git-remove-file.sh
Last active September 22, 2016 17:32
Show Gist options
  • Save k4gdw/04768c293c7366ccc307ae9bb562acb5 to your computer and use it in GitHub Desktop.
Save k4gdw/04768c293c7366ccc307ae9bb562acb5 to your computer and use it in GitHub Desktop.
remove file from git history
#!/bin/bash
#-------------------------------------------------------------------------------
# Name : git-remove-file.sh
# Written By: David Underhill, with modifications by, Bryan K. Johns, K4GDW
# Purpose : Script to permanently delete files / folders from your git
# : repository. To use it, cd to the root of your repository and then
# : run the script as shown.
# Calls : ./git-sanitize.sh path1 path2...
# Comments : This is my version of the git-delete-history script found here:
# : http://k4gdw.us/git-sanitize
# Date/Time : Thursday, September 22, 2016 11:17:38 AM
#-------------------------------------------------------------------------------
set -o errexit
if [ $# -eq 0 ]; then
echo "Error: Must supply at least one file path to be scrubbed from the repo."
exit 1
fi
# make sure we're at the root of git repository
if [ ! -d .git ]; then
echo "Error: Must run this script from the root of a git\n repository"
exit 1
fi
# Remove all paths passed as arguments from the history of the
# repository.
files=$@
git filter-branch --index-filter \
"git rm -rf --cached --ignore-unmatch $files" \
HEAD
# Remove the temporary history git-filter-branch otherwise it gets
# left behind for a long time.
rm -rf .git/refs/original/
&& git reflog expire --expire=now --all
&& git gc --aggressive --prune=now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment