parent branch / base branch
Unfortunately, it is not possible to find the name of the parent/base branch.
Git stores commits and pointers, not branch ancestry.
That means:
- a branch is just a movable name that points to one commit
- Git knows commit parents, but not "this branch was created from that branch"
- after more commits, rebases, merges, or deleted branches, that information is gone
But Git cannot reliably tell you:
- "feature-2 was created from feature-1"
If you are not asking Git about branch ancestry, but the hosting platform about the current PR, then CLI tools can help:
- GitHub:
gh pr view --json baseRefName --jq .baseRefName - GitLab:
glab mr view --output json | jq -r .target_branch- Codeberg:berg --output json --non-interactive pull list --state open | jq -r --arg branch "$(git branch --show-current)" '[.[] | select(.head.ref == $branch)][0].base.ref'