#!/usr/bin/env bash # Usage: git filter-branch -f --parent-filter ~/git-filters/parent-prune-empty-first HEAD has_empty_tree() { [[ "$(git rev-parse "${1}^{tree}")" == 4b825dc642cb6eb9a060e54bf8d69288fbee4904 ]] } has_parents() { git rev-parse "${1}^" &>/dev/null } for parent in $(cat); do if [[ "$parent" == '-p' ]]; then continue fi if has_empty_tree "$parent" && ! has_parents "$parent"; then continue fi printf ' -p %s ' "$parent" done echo