Sytone's Ramblings

The occasional posts of a guy who plays with technology.

Using git aliases

2019-01-17 4 min read Development

I work with git for home and work project, I recently found you can use alias across all platforms due to the way it runs on windows. So all these aliases will work happily on git for windows and hopefully will make you work day more streamlined.

Some are from sites I have found and other I have created myself.

To access the global git configuration run the following command and then add this to the bottom if you do not have a alias section already. This command will use your default editor.

git config --global -e

These are all the aliases, just cut and paste into the configuration.

[alias]
  co = checkout
  ec = config --global -e
  ecl = config -e
  up = !git pull --rebase --prune $@ && git submodule update --init --recursive
  cob = checkout -b

  # Create a new branch with static naming and switch to it.
  cobm = "!f() { git checkout -b users/$USERNAME/$1; git push --set-upstream origin users/$USERNAME/$1; }; f"

  cm = !git add -A && git commit -m
  cmp = "!f() { msg=${1-Lazy commit and push}; echo \"Commiting with message: $msg\"; git add -A; git commit -m \"$msg\"; git push; }; f"
  save = !git add -A && git commit -m 'SAVEPOINT'
  wip = commit -am "WIP"
  undo = reset HEAD~1 --mixed
  amend = commit -a --amend

  # Want to start again, this will add you current changes and then reset the HEAD. You can always get back the files you abandoned.
  wipe = !git add -A && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard

  # To get the WIPE changes back again.
  unwipe = "f() { SavePoint=$(git reflog --pretty=format:'%h|%s' | grep 'WIPE SAVEPOINT'); IFS='|'; read -ra HASHES <<< $SavePoint; git checkout $HASHES; }; f"

  # This will delete any branches already merged to master to keep you local repo clean.
  bclean = "!f() { git checkout ${1-master} && git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs git branch -d; }; f"

  # Run this after your PR is in to switch back to master and cleanup branches.
  bdone = "!f() { git checkout ${1-master} && git up && git bclean ${1-master}; }; f"

  logg = log --oneline --graph --color --decorate
  pid = !git config user.email 'ENTERPERSONALEMAILADDRESS' && git config user.name 'ENTERPERSONALUSERNAME' && git config user.email && git config user.name
  wid = !git config user.email 'ENTERWORKEMAILADDRESS' && git config user.name 'ENTERWORKUSERNAME' && git config user.email && git config user.name
  whois = "!sh -c 'git log -i -1 --pretty=\"format:%an <%ae>\n\" --author=\"$1\"' -"
  whatis = show -s --pretty='tformat:%h (%s, %ad)' --date=short
  graphviz = "!f() { echo 'digraph git {' ; git log --pretty='format:  %h -> { %p }' \"$@\" | sed 's/[0-9a-f][0-9a-f]*/\"&\"/g' ; echo '}'; }; f"
  aliases = !git config --get-regexp 'alias.*' | cut -c 7- | sed 's/[ ]/ = /'
  lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
  open = "!f() { REPO_URL=$(git config remote.origin.url); explorer ${REPO_URL%%.git}; }; f"
  browse = !git open
  churn = !git log --all --find-copies --find-renames --name-only --format='format:' "$@" | sort | grep -v '^$' | uniq -c | sort | awk 'BEGIN { print \"count\tfile\" } {print $1 \"\t\" $2}' | sort -g
  remote-merged = "!f() { for branch in $(git branch -r --merged | grep -v HEAD);	do echo -e $(git show --format=\"%ci %cr %an\" $branch | head -n 1) \\t$branch; done }; f | sort -r"
  remote-merged-me = "!f() { for branch in $(git branch -r --merged | grep -v HEAD); do echo -e $(git show --format=\"%ci %cr %an\" $branch | head -n 1) \\t$branch; done }; f | grep $USERNAME | sort -r"
  remote-merged-query = "!f() { for branch in $(git branch -r --merged | grep -v HEAD | grep -i \"$1\"); do echo -e $(git show --format=\"%ci %cr %an\" $branch | head -n 1) \\t$branch; done }; f"
  remote-age = "!f() { for ref in $(git for-each-ref --sort=-committerdate --format=\"%(refname:short)\" refs/remotes); do git log -n1 $ref --pretty=format:'%Cgreen%cr%Creset | %C(yellow)%d%Creset% | %C(bold cyan)%an%Creset%n'; done }; f"