#!/bin/bash set -eu [ -n "${DEBUG:-}" ] && set -x # use cli arg base_branch=${1:-} # detect base branch (when git-new was used) [ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short refs/bases/"$(git symbolic-ref --short HEAD)" 2>/dev/null || :) # detect trunk via origin/HEAD [ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null || :) # fallback to base branch => current branch [ -n "$base_branch" ] || base_branch=$(git symbolic-ref --short HEAD 2>/dev/null || :) # resolve upstream if [[ "$base_branch" = "origin/"* ]]; then upstream=$base_branch else upstream=$(git rev-parse --abbrev-ref --symbolic-full-name "$base_branch"'@{u}') fi [ -n "${base_branch:?}" ] || exit 1 [ -n "${upstream:?}" ] || exit 1 echo "Syncing with $upstream (base: $base_branch)" # fetch upstream git fetch ${upstream/\// } # check if base and upstream have diverged if ! git diff --quiet "$upstream" "$base_branch"; then git diff --stat "$upstream" "$base_branch" fi # rebase git rebase --interactive "$upstream"