Last active
February 18, 2022 16:17
-
-
Save braitsch/6500890 to your computer and use it in GitHub Desktop.
Bash script to nuke unwanted files from the history of a git repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bash script to nuke unwanted files from the history of a git repository | |
# Modification of David Underhill's script http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/ | |
# http://stackoverflow.com/questions/2100907/how-do-i-purge-a-huge-file-from-commits-in-git-history | |
# usage : execute from repository root ./git-purge.sh "path to unwanted files" | |
#!/bin/bash | |
set -o errexit | |
if [ $# -eq 0 ]; then | |
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 all files/paths passed as arguments from the history of the repo | |
files=$@ | |
git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch $files" --prune-empty --tag-name-filter cat -- --all | |
# remove the temporary history git-filter-branch otherwise it will persist in the cache | |
rm -rf .git/refs/original/ | |
git reflog expire --expire=now --all | |
git gc --prune=now | |
git gc --aggressive --prune=now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment