A perhaps simpler (recent) alternative (mine): https://stackoverflow.com/a/79132926/2039709
git merge-base main $1 \ | xargs -I{} git branch --sort=creatordate --contains {} \ | grep -Ev "main|$1" \ | head -n 1