Skip to content

Instantly share code, notes, and snippets.

@hackerzgz
Last active May 27, 2020 03:24
Show Gist options
  • Save hackerzgz/9425e5e1bd2dbdbc0d85a8aedb667256 to your computer and use it in GitHub Desktop.
Save hackerzgz/9425e5e1bd2dbdbc0d85a8aedb667256 to your computer and use it in GitHub Desktop.
clean up top large files in git commit history
#!/bin/bash
# INSPIRED BY:
# http://luren5.com/2017/05/11/%E6%8F%90%E4%BA%A4%E5%A4%A7%E6%96%87%E4%BB%B6%E5%AF%BC%E8%87%B4-git-objects-pack%E7%9B%AE%E5%BD%95%E5%BE%88%E5%A4%A7%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/
TOP_FILES=top-files
CLEAN_FILES=clean-files
function find_top_files() {
TOP=$1
git rev-list --objects --all | \
grep "$(git verify-pack -v .git/objects/pack/*.idx \
| sort -k 3 -n | tail -${TOP} | awk '{print$1}')" \
> "${TOP_FILES}"
}
function clean_files() {
cat "${TOP_FILES}" | awk '{print $2}' | tr '\n' ' ' \
> "${CLEAN_FILES}"
git filter-branch -f --prune-empty --index-filter \
"git rm -rf --cached --ignore-unmatch `cat ${CLEAN_FILES}`" \
--tag-name-filter cat -- --all
if [[ $? -eq 0 ]]; then
echo "clean big files successful, push to remote origin (e.g.):"
echo -e "\n\tgit push origin --force --all"
rm "${TOP_FILE}" "${CLEAN_FILES}"
fi
}
if [[ $1 == "-h" ]];
then
echo "usage:"
echo -e "\tclean_git_history.sh find_top_files \$TOP"
echo -e "\tclean_git_history.sh clean_files"
else
$@
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment