Skip to content

Instantly share code, notes, and snippets.

@skolima
Last active May 1, 2026 14:20
Show Gist options
  • Select an option

  • Save skolima/3ba15ad438b7562a4513e99a4baf216d to your computer and use it in GitHub Desktop.

Select an option

Save skolima/3ba15ad438b7562a4513e99a4baf216d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Fast-forward every git repo immediately under $PWD on its default branch.
# Skips repos with uncommitted changes, no `origin` remote, or a `.git` file
# (i.e. worktrees — only the host repo gets updated).
yellow=$'\e[33m' red=$'\e[31m' green=$'\e[32m' reset=$'\e[0m'
shopt -s nullglob
for repo in */.git/; do
dir=${repo%/.git/}
printf '\n%s%s%s\n' "$yellow" "$dir" "$reset"
(
cd "$dir" || exit 1
if ! git diff --quiet HEAD -- 2>/dev/null; then
printf ' %sskipped — uncommitted changes%s\n' "$red" "$reset"
exit 0
fi
default=$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null)
if [ -z "$default" ]; then
printf ' %sskipped — no origin remote%s\n' "$red" "$reset"
exit 0
fi
git checkout --quiet "${default#origin/}" \
&& git pull --ff-only --prune --quiet \
|| printf ' %sfast-forward failed%s\n' "$red" "$reset"
)
done
printf '\n%sDone.%s\n' "$green" "$reset"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment