Skip to content

Instantly share code, notes, and snippets.

@dekarrin
Created June 8, 2014 15:51
Show Gist options
  • Save dekarrin/f795427e183fc8976a37 to your computer and use it in GitHub Desktop.
Save dekarrin/f795427e183fc8976a37 to your computer and use it in GitHub Desktop.
Totally remove file from repository history
#!/bin/bash
TEMP_DIR=".git_annhilate_temp"
if [[ $3 == "" ]]
then
echo Usage: $0 ssh-repo-url branch file-to-delete
else
F=0 #failure check
REPO_URL=$1
BRANCH=$2
DELETE_FILE=$3
git clone $REPO_URL $TEMP_DIR || let "F |= 1"
cd $TEMP_DIR
if [[ $BRANCH == "master" ]]
then
git checkout $BRANCH || let "F |= 1"
fi
git filter-branch --index-filter "git rm --cached --ignore-unmatch $DELETE_FILE" --prune-empty --tag-name-filter cat -- --all || let "F |= 1"
git push --tags --force origin $BRANCH || let "F |= 1"
cd ..
rm -rf $TEMP_DIR
if [[ $F == 0 ]]
then
echo ""
echo "File removed from git history."
echo "WARNING: All clones/forks based on origin must be recloned/rebased or merge conflicts may occur!"
fi
fi
TEMP_DIR=""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment