Skip to content

Instantly share code, notes, and snippets.

@cgoldberg
Last active April 25, 2025 15:28
Show Gist options
  • Save cgoldberg/90d681e97c78a19ebd955d58c85a1677 to your computer and use it in GitHub Desktop.
Save cgoldberg/90d681e97c78a19ebd955d58c85a1677 to your computer and use it in GitHub Desktop.
Bash - pull all local git branches in current repo
#!/usr/bin/env bash
# pull all local git branches in current repo
# - stashes uncommitted changes
# - switches to default branch and does pull/rebase
# - switches to every other local branch and does pull/rebase
# - switches back to original branch
# - only works if in a git repo
set -e
die() { echo "$*" 1>&2 ; exit 1; }
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
die "not in a git repo"
fi
echo
current_branch=$(git branch --show-current)
default_branch=$(git remote show origin | sed -n "/HEAD branch/s/.*: //p")
git stash --quiet
echo "switching to branch '${default_branch}'"
git checkout --quiet "${default_branch}"
echo "pulling '${default_branch}'"
git pull --rebase --stat
echo
branches=($(git branch --format="%(refname:short)"))
branches_without_default=(${branches[@]/${default_branch}})
for branch in "${branches_without_default[@]}"; do
echo "switching to branch '${branch}'"
git checkout --quiet "${branch}"
echo "pulling '${branch}'"
git pull --rebase --stat
echo
done
git checkout --quiet "${current_branch}"
git stash pop --quiet 2>/dev/null || true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment