Skip to content

Instantly share code, notes, and snippets.

@andrewh
Last active December 17, 2015 06:48
Show Gist options
  • Save andrewh/5567695 to your computer and use it in GitHub Desktop.
Save andrewh/5567695 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -e
FILES_TO_PRUNE="path_to_large_file_from_root_of_repo"
# FILES_TO_PRUNE="${FILES_TO_PRUNE} <full_path_from_repo_root>"
catch_error () {
local cmd_name=$1
local ret_code=$2
if [ $ret_code -ne 0 ]; then
echo "Problem found, please examine ${cmd_name} output."
exit 1
else
echo "Command ${cmd_name} completed without errors."
fi
}
go_up_one_directory () {
cd ..
}
git_filter_unwanted_files() {
local files_to_prune=$1
printf "Filtering this git repo...\n"
git filter-branch --prune-empty --index-filter \
"git rm --cached --ignore-unmatch ${files_to_prune}" \
--tag-name-filter cat -- --all
catch_error "git filter-branch" $?
}
git_clone_filtered_repo () {
local working_dir=$1
local new_repo="$(basename $working_dir)-cleaned"
printf "\nCloning into a new repo...\n"
git clone --no-hardlinks file://$working_dir $new_repo
catch_error "git clone" $?
}
echo -n 'Are you sure you want to do git filter-branch on this repo? (y/n) '
read input
case $input in
[Yy])
git_filter_unwanted_files $FILES_TO_PRUNE
go_up_one_directory
git_clone_filtered_repo $OLDPWD
;;
*)
echo "Exiting."
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment