Skip to content

Instantly share code, notes, and snippets.

@jmatsu
Last active August 1, 2020 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmatsu/3039c92ff61d75fae8abff1eb65ccb6f to your computer and use it in GitHub Desktop.
Save jmatsu/3039c92ff61d75fae8abff1eb65ccb6f to your computer and use it in GitHub Desktop.
To avoid default branch name chaos
# get the default branch of the current repository. NOTE that this will requires the network connection
git config --global alias.default-branch "\!f() { git ls-remote --symref \$(git remote get-url origin) HEAD | grep 'refs/heads' | awk '\$0=\$2' | sed 's/refs\/heads\///'; }; f"
# then run git default-branch
# All commands are executable in any local repositories
# read the branch name cache of the current repository. config section is `misc`.
git config --global alias.get-branch-cache '!f() { git config --local misc.$1-branch; }; f'
# store the branch name cache of the current repository. config section is `misc`.
git config --global alias.save-branch-cache '!f() { git config --local misc.$1-branch "$2"; }; f'
# get the default branch name of the current repository
git config --global alias.get-default-branch '!f() { local -r def_branch="$(git get-branch-cache default)"; if [[ -z "$def_branch" ]]; then git refresh-default-branch; fi; git get-branch-cache default; }; f'
# refresh the default branch cache of the current repository
git config --global alias.refresh-default-branch '!f() { git save-branch-cache default "$(git default-branch)"; }; f'
# switch and pull the latest specified branch
git config --global alias.latest-any '!f() { git switch $1 && git fetch origin $1 && git merge --ff origin/$1;}; f'
# switch and pull the latest default branch
git config --global alias.latest-default '!f() { git latest-any "$(git get-default-branch)"; }; f'
# They depend on the specific properties of the current repository
# if develop is the default development branch of the repository
git config --local alias.latest-dev 'mv2latest develop'
# if release is the release branch of the repository
git config --local alias.latest-release 'mv2latest release'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment