Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manuelnaranjo/e6d2dba90fe47c3e01e28b2e2287e5f3 to your computer and use it in GitHub Desktop.
Save manuelnaranjo/e6d2dba90fe47c3e01e28b2e2287e5f3 to your computer and use it in GitHub Desktop.
Fix broken repo
#!/usr/bin/env bash
function clean_invalid_refs() {
INVALID_SHAS=`git for-each-ref --format="%(objectname)" | sort | uniq | git cat-file --batch-check | grep missing | cut -d' ' -f 1`;
if [ -z "${INVALID_SHAS}" ]; then
echo "Nothing to cleanup" > /dev/stderr
return
fi
REGEX=$(echo "$INVALID_SHAS" | paste -sd '|')
REFS=`git for-each-ref --format="%(refname) %(objectname)" | grep -E $REGEX | cut -d' ' -f 1`
for ref in $REFS; do
echo "deleting ${ref}"
git update-ref -d ${ref}
done
}
clean_invalid_refs
git fetch "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment