Skip to content

Instantly share code, notes, and snippets.

@ziyunli
Created November 21, 2019 16:07
Show Gist options
  • Save ziyunli/9afb3e44a7fe665d83d8992688d7d252 to your computer and use it in GitHub Desktop.
Save ziyunli/9afb3e44a7fe665d83d8992688d7d252 to your computer and use it in GitHub Desktop.
Clean up invalid git branches
#!/bin/sh
# From https://stackoverflow.com/a/28319271
set -e
if [ $# -eq 0 ]; then
dir="."
else
dir="$1"
fi
if [ ! -d "$dir" ]; then
echo "not a dir: $dir"
exit 1
fi
if [ ! -d "$dir/.git" ]; then
echo "not a git repo: $dir"
exit 1
fi
cd "$dir"
files=$(find .git/refs -type f)
for f in $files; do
id=$(cat "$f")
if ! git rev-parse --quiet "$id" \
>/dev/null 2>&1; then
continue
fi
if ! git rev-parse --quiet --verify "$id^{commit}" \
>/dev/null 2>&1; then
echo "Removing ref $f with missing commit $id"
rm "$f"
fi
done
if [ ! -f .git/packed-refs ]; then
exit 0
fi
packfiles=$(cat .git/packed-refs \
| grep -v '#' \
| awk '{print $2}')
for f in $packfiles; do
if ! git rev-parse --quiet --verify "$f" \
>/dev/null 2>&1; then
continue
fi
id=$(git rev-parse "$f")
if ! git rev-parse --quiet --verify "$id" \
>/dev/null 2>&1; then
continue
fi
if ! git rev-parse --quiet --verify "$id^{commit}" \
>/dev/null 2>&1; then
echo "Removing packed ref $f with missing commit $id"
git update-ref -d $f
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment