USR_BIN="$(dirname "$0")" #or "/usr/bin`, or "~/../usr/bin",
cat << EOF > "${USR_BIN}/default_branch.sh"
#!/bin/sh
#/* Usage: "\$(default_branch.sh)" */
DEFAULT_BRANCH="\$(basename \$(git symbolic-ref --short refs/remotes/\$(git remote)/HEAD))" #remote branch
if [ ! -n "\${DEFAULT_BRANCH}" ]; then #Remoteless path
DEFAULT_BRANCH="\$(git branch --sort=-refname | grep -o -m1 '\b\(main\|master\|trunk\)\b')" #local branch
fi
echo "\${DEFAULT_BRANCH}"
EOF
chmod u+x "${USR_BIN}/default_branch.sh"
Source: https://github.com/SwuduSusuwu/SubStack/blob/trunk/Macros.sh#L87 (SUSUWU_DEFAULT_BRANCH()
).
Disclaimer: am author.
Used as; git config --global alias.switchdefault '!git switch $(default_branch.sh)'
(example is to alias git switchdefault
to git switch trunk
, or git switch main
, or git switch master
, based on which of those the Git repository uses).