#!/usr/bin/env bash

usage() {
cat <<HERE
usage: git alias                         # list all aliases
   or: git alias <search-pattern>        # show aliases matching pattern
   or: git alias <alias-name> <command>  # alias a command
HERE
}

case $# in
  0) git config --get-regexp 'alias.*' | sed 's/^alias\.//' | sed 's/[ ]/ = /' | sort ;;
  1) git alias | grep -e "$1" ;;
  2) git config --global alias."$1" "$2" ;;
  *) >&2 echo "error: too many arguments." && usage && exit 1 ;;
esac
