Skip to main content

git/_get_primary_branch

1#!/usr/bin/env bash
2# Prints the name of the primary branch, e.g. main or live
4set -o errexit
5set -o nounset
7GIT_ROOT=$(git rev-parse --absolute-git-dir)
9if [[ -f "$GIT_ROOT/refs/remotes/origin/HEAD" ]]
10then
11 PRIMARY_BRANCH=$(cat "$GIT_ROOT/refs/remotes/origin/HEAD" \
12 | tr '/' ' ' \
13 | awk '{print $5}')
14elif [[ -f "$GIT_ROOT/refs/remotes/origin/live" ]]
15then
16 PRIMARY_BRANCH="live"
17else
18 PRIMARY_BRANCH="main"
19fi
21echo -e "$PRIMARY_BRANCH"