Last active
October 22, 2024 15:35
-
-
Save andrewh/5567695 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
set -e | |
FILES_TO_PRUNE="path_to_large_file_from_root_of_repo" | |
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