Skip to content

Instantly share code, notes, and snippets.

@JrGoodle
Last active October 17, 2015 09:22
Show Gist options
  • Save JrGoodle/de4dce2893d128571b6c to your computer and use it in GitHub Desktop.
Save JrGoodle/de4dce2893d128571b6c to your computer and use it in GitHub Desktop.
Minify git repo
#! /bin/bash
# Reference: http://naleid.com/blog/2012/01/17/finding-and-purging-big-files-from-git-history
# Get unique SHA's for each object throughout it’s history.
git_allfileshas()
{
git rev-list --objects --all | sort -k 2 > allfileshas.txt
}
# Get the last object SHA for all committed files and sort them in biggest to smallest order
git_bigobjects()
{
git gc && git verify-pack -v .git/objects/pack/pack-*.idx | egrep "^\w+ blob\W+[0-9]+ [0-9]+ [0-9]+$" | sort -k 3 -n -r > bigobjects.txt
}
# List all files in repo history from biggest to smallest
git_bigtosmall()
{
for SHA in `cut -f 1 -d\ < bigobjects.txt`; do
echo $(grep $SHA bigobjects.txt) $(grep $SHA allfileshas.txt) | awk '{print $1,$3,$7}' >> bigtosmall.txt
done
}
# Create lists of files and shas by size
git_minify()
{
git_allfileshas
git_bigobjects
git_bigtosmall
}
# Use filter-branch to remove the file/directory
# Pass file to remove as argument to function
git_filterbranch()
{
git filter-branch --prune-empty --index-filter "git rm -rf --cached --ignore-unmatch $1" --tag-name-filter cat -- --all
}
# clone the repo and make sure to not leave any hard links
#
git_clone_minified()
{
git clone --no-hardlinks "file:///Users/goodle/github/$1" "$1-minified"
}
# You can use this command from the parent directory that contains your git repository and it’s clone to see how much space each of them take, and how much you’ve shrunk the repo in size:
# du -s *(/)
# du -sh *(/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment