Skip to content

Instantly share code, notes, and snippets.

@cgoldberg
Last active December 16, 2025 22:37
Show Gist options
  • Select an option

  • Save cgoldberg/35715354ce7674c44f67a8b70f9901d7 to your computer and use it in GitHub Desktop.

Select an option

Save cgoldberg/35715354ce7674c44f67a8b70f9901d7 to your computer and use it in GitHub Desktop.
Bash/Git - show local branches and how far ahead/behind they are from remote/origin
#!/usr/bin/env bash
# show local branches and how far ahead/behind they are from remote/origin
git fetch origin
for branch in $(git branch --format='%(refname:short)'); do
remote_branch="origin/$branch"
if git show-ref --verify --quiet refs/remotes/$remote_branch; then
ahead=$(git rev-list --count $remote_branch..$branch)
behind=$(git rev-list --count $branch..$remote_branch)
echo "$branch -> $remote_branch : ahead $ahead, behind $behind"
else
echo "$branch -> $remote_branch : does not exist on remote"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment