Skip to content

Instantly share code, notes, and snippets.

@leedm777
Last active August 29, 2015 14:20
Show Gist options
  • Save leedm777/bbd9f08390c324c79283 to your computer and use it in GitHub Desktop.
Save leedm777/bbd9f08390c324c79283 to your computer and use it in GitHub Desktop.
Erases a file from the history of the current branch
#!/bin/sh
#
# Removes a file from git history of the current branch
#
PROGNAME=$(basename $0)
REMOVE=$1
REPO_NAME=$(basename $(pwd))
if test -z ${REMOVE}; then
echo "usage: ${PROGNAME} [file-to-erase-from-history]" >&2
exit 1
fi
if ! test -d .git; then
echo "${PROGNAME}: Must run from git repo" >&2
exit 1
fi
set -ex
git filter-branch --force --index-filter \
"git rm -r --cached --ignore-unmatch ${REMOVE}" \
--prune-empty
#
# Push to a fresh bare repo to get a sense of how small it is now
#
rm -rf /tmp/${REPO_NAME}
git init --bare /tmp/${REPO_NAME}
git push /tmp/${REPO_NAME} HEAD:master
du -sh /tmp/${REPO_NAME}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment