Skip to content

Instantly share code, notes, and snippets.

@timmc-edx
Created February 11, 2022 22:52
Show Gist options
  • Save timmc-edx/786dc5429d6c60e4344e6b83d2aab638 to your computer and use it in GitHub Desktop.
Save timmc-edx/786dc5429d6c60e4344e6b83d2aab638 to your computer and use it in GitHub Desktop.
Aliases for fetch or pulling the git default branch
function git_default_branch {
# Guess the name of the remote -- either origin or the first other remote
guessed_remote="$(
(
git remote | grep -P '^origin$'
git remote | grep -P '^origin$' -v
) | head -n1 | tr -d '\n'
)"
git symbolic-ref "refs/remotes/$guessed_remote/HEAD" \
| sed 's|^refs/remotes/[^/]\+/||'
}
alias gpm="git checkout \$(git_default_branch) && git pull"
alias gfm="git fetch origin && git branch -f \$(git_default_branch) origin/\$(git_default_branch)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment