fix-git-refs.sh
876 Bytes
#!/bin/sh
# Fix Windows/Git branch ref sync bug
# On Windows, git commit may fail to update loose refs properly,
# causing branch refs to fall back to stale packed-refs values.
# This script reads the correct hash from the reflog and writes it
# to the loose ref file, ensuring branch refs stay in sync.
GIT_DIR="$(git rev-parse --git-dir 2>/dev/null)"
if [ -z "$GIT_DIR" ]; then
exit 0
fi
BRANCH="$(git symbolic-ref HEAD 2>/dev/null | sed 's|refs/heads/||')"
if [ -z "$BRANCH" ]; then
exit 0
fi
REFLOG="$GIT_DIR/logs/refs/heads/$BRANCH"
if [ -f "$REFLOG" ]; then
NEW_HASH="$(tail -1 "$REFLOG" | cut -d' ' -f2)"
else
NEW_HASH="$(tail -1 "$GIT_DIR/logs/HEAD" | cut -d' ' -f2)"
fi
if [ -z "$NEW_HASH" ]; then
exit 0
fi
REF_FILE="$GIT_DIR/refs/heads/$BRANCH"
REF_DIR="$(dirname "$REF_FILE")"
mkdir -p "$REF_DIR" 2>/dev/null
printf '%s\n' "$NEW_HASH" > "$REF_FILE"