Skip to content

Instantly share code, notes, and snippets.

@stefansundin
Created August 13, 2015 00:17
Show Gist options
  • Save stefansundin/f386d3d2a0e1aa6e5c5a to your computer and use it in GitHub Desktop.
Save stefansundin/f386d3d2a0e1aa6e5c5a to your computer and use it in GitHub Desktop.
Git nuke script to remove specified files from all branches and tags.
#!/bin/bash
set -o errexit
# BE VERY CAREFUL USING THIS SCRIPT. MAKE A BACKUP (OR TWO) FIRST WITH:
# git clone --mirror ...
# Install by putting this file in e.g. /usr/local/bin/git-nuke
# chmod +x /usr/local/bin/git-nuke
if [ $# -eq 0 ]; then
echo "Usage: git nuke path1 path2"
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 paths from all branches and tags
for i in "$@"; do
echo "Removing: $i"
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch '$i'" --tag-name-filter cat -- --all
done
# remove old reflogs
echo
echo "Cleaning up"
rm -rf .git/refs/original/ && git reflog expire --all && git gc --aggressive --prune
echo
echo "You may want to force-push with:"
echo "git push --all --force"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment