Skip to content

Instantly share code, notes, and snippets.

@alexanderscott
Created September 7, 2014 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderscott/772c1c5802c5b3204afb to your computer and use it in GitHub Desktop.
Save alexanderscott/772c1c5802c5b3204afb to your computer and use it in GitHub Desktop.
remove file from git history, prune & garbage collect, push removal to origin
#!/bin/bash
# Get filename from cmd-line arg
filename=$1
# Fetch all remote branches locally and setup tracking
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
git branch --track ${branch##*/} $branch
done
# Remove file from git history & garbage collect
git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch $filename' --prune-empty -f -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
# Push updates
git push origin --force --all
git push origin --force --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment