Skip to content

Instantly share code, notes, and snippets.

@phillipalexander
Created August 31, 2013 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipalexander/6400394 to your computer and use it in GitHub Desktop.
Save phillipalexander/6400394 to your computer and use it in GitHub Desktop.
Banish a file or directory (and any record of it ever existing) from your git repository
#!/bin/bash
set -o errexit
 
# Author: David Underhill
# Script to permanently delete files/folders from your git repository. To use
# it, cd to your repository's root and then run the script with a list of paths
# you want to delete, e.g., git-forever-remove path1 path2
 
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 paths passed as arguments from the history of the repo
files=$@
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch $files" HEAD
 
# remove the temporary history git-filter-branch otherwise leaves behind for a long time
rm -rf .git/refs/original/ && git reflog expire --all && git gc --aggressive --prune
#!/bin/sh
#####################################################################
# Program: git banish
#####################################################################
# Version: 1.0.0
# Date: 2013-08-31 13:08:23
# Author: Phillip Alexander (github.com/phillipalexander)
#
# Notes: See the excelent github article on this topic for more info
# https://help.github.com/articles/remove-sensitive-data
#
#
#####################################################################
#--------------------------------------------------------------------
# Setup Global Variables
#--------------------------------------------------------------------
files=$@
#--------------------------------------------------------------------
# Test for prerequisites
#--------------------------------------------------------------------
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
#--------------------------------------------------------------------
# Banish
#--------------------------------------------------------------------
# This command will run the entire history of every branch and tag, changing any commit that involved the file Rakefile, and any commits afterwards. Commits that are empty afterwards (because they only changed the Rakefile) are removed entirely. Note that you'll need to specify the path to the file you want to remove, not just its filename.
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch $files" --prune-empty --tag-name-filter cat -- --all
# Now that we've erased the file from history, let's ensure that we don't accidentally commit it again.
# Please note that this will overwrite your existing tags.
echo "$files" >> .gitignore && git add .gitignore && git commit -m "Add $files to .gitignore"
# While git filter-branch rewrites the history for you, the objects remain in your local repository until they've been dereferenced and garbage collected. If you are working in your main repository, you might want to force these objects to be purged.
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