#!/bin/bash set -euo pipefail if [ $# -ne 1 ]; then echo "Usage: git rebase-remote " >&2 exit 1 fi remote_branch="$1" remote="${remote_branch%%/*}" branch="${remote_branch#*/}" if [ "$remote" = "$branch" ]; then echo "Error: Could not parse remote/branch from '$remote_branch'" >&2 exit 1 fi # Save current branch/commit current=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || git rev-parse HEAD) # Fetch both branches git fetch "$remote" "$branch" git fetch origin main # Checkout remote branch in detached HEAD state, rebase, and push git checkout --detach "$remote/$branch" git rebase origin/main git push --force-with-lease "$remote" "HEAD:$branch" # Return to original branch/commit git checkout "$current" echo "✓ Rebased and pushed $remote_branch"