Skip to content

Instantly share code, notes, and snippets.

@ArtMin96
Last active September 18, 2025 06:27
Show Gist options
  • Save ArtMin96/127d009b8aa4cbd35211d94b6adee5e2 to your computer and use it in GitHub Desktop.
Save ArtMin96/127d009b8aa4cbd35211d94b6adee5e2 to your computer and use it in GitHub Desktop.
Git script to find parent branch of a branch
#!/bin/sh
#
# git-find-parent.sh
# Find the most likely parent branch for a given feature branch.
#
branch="$1"
# If no branch passed, use current branch
if [ -z "$branch" ]; then
branch=$(git rev-parse --abbrev-ref HEAD)
fi
# Search local + remote branches except the branch itself
git for-each-ref --format='%(refname:short)' refs/heads/ refs/remotes/ \
| grep -v "^$branch$" \
| while read b; do
base=$(git merge-base "$b" "$branch" 2>/dev/null) || continue
[ -n "$base" ] && echo "$(git show -s --format='%ct %h %s' "$base") $b"
done \
| sort -n \
| tail -n1 \
| awk '{gsub(/^origin\//,"",$NF); print $NF}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment