5 new Git commands and 1 tip you’ll use every day

You may know you can add your own sub-commands to git, by having a command in your path which is name git-${subcommand}
.
Here’s some I use every day to get more work done.
git-main
when you’re not sure which convention is used, main or master.
#!/bin/sh
set -eu
# print name of main branch, e.g. "main" :) or "master" :(
git remote show origin|grep HEAD|sed 's/.* //'
git-sync
for when you want to get into sync with main.
#!/bin/sh
set -eu
# sync current branch with main
main=$(git main)
git checkout $main
git pull
git checkout -
git pull
git merge --no-edit $main
git-tmp
if you want to quickly work on something.
#!/bin/sh
set -eu
# create a new tmp branch
git checkout $(git main)
git branch -D tmp || true
git checkout -b tmp
git-tidy
for when things get in a mess.
#!/bin/sh
set -eu
# delete all local branches not on remote
git fetch -p && for branch in `git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'`; do git branch -D $branch; done
# delete tags not on remote
git fetch --prune origin '+refs/tags/*:refs/tags/*'
git-release
when you’re releasing several times a day with automation that is trigger by the tag.
#!/bin/sh
set -eu
# trigger a CI release by tagging with next patch version
tag=$(git describe --tags --abbrev=0)
patch=$(echo "$tag" | grep -o '[0-9]*$')
prefix=${tag%$patch}
next_patch=$(( $patch + 1))
next_tag="$prefix$next_patch"
echo "$tag -> $next_tag - press enter to tag and push"
read
git tag $next_tag
git push origin $next_tag
And the tip? You can also alias sub-commands in git:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
Now you can run git co main
!