-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-get-default-branch
executable file
·37 lines (34 loc) · 1.22 KB
/
git-get-default-branch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash
# Author: github.com/danielhoherd
# License: MIT
# Purpose: for each remote, print: <name of remote> <default branch>
# EG: $ git-get-default-branch
# origin master
# github gh-pages
#
# If the repo default branch has been renamed, you must update it by running something like:
# git remote | xargs -r -I {} git remote set-head {} -a
#
# If you just want to check out the default branch really quickly:
# git checkout $(git branch -r | awk -F/ '/HEAD/ {print $NF}')
#
# Or if you want an alias to check out the default branch quickly::
# git config --global alias.co-default '!'"git checkout \$(git branch -r | awk -F/ '/HEAD/ {print \$NF}')"
if [[ "${#@}" -gt 0 ]] ; then
remotes=( "${@}" )
else
# In 2023, macOS 13 still has bash 3.2.57, which was released in 2014 🙄
if [[ "${BASH_VERSINFO[0]}" -lt 4 ]] ; then
remotes=()
while IFS='' read -r line ; do
remotes+=("$line")
done < <(git remote)
else
mapfile -t remotes < <(git remote)
fi
fi
for remote in "${remotes[@]}" ; do
git remote set-head "$remote" -a >/dev/null
branch=$(git ls-remote --symref "$remote" HEAD | awk '$2 ~ /^refs/ { sub(".*/","", $2) ; print $2 }')
echo "$remote $branch"
done