Useful Git Command Aliases

Here are a couple git command aliases I’ve added to my .gitconfig file which may be generally useful. If you combine with Git auto-complete, these will be included as well.

Quick overview:

  • find-merge / show-merge  – used to help trace problematic merges / conflicts (credit: Stack overflow thread on tracing merges)
  • pull-all – pulls and fast-forwards all local branches that can be fast-forwarded.  Keeps me from accidentally making a local commit over some remote change.
  • tags-recent – by default git tag –list lists ALL tags in chronological order, which is pretty painful; this shows you only the most recent 10.

To make these available, add the following block to the .gitconfig file in your home directory.


[alias]
# find when commits were merged
# USAGE:
# git find-merge <SHA-1> // when merged to current branch
# git find-merge <SHA-1> master // when merged to master
find-merge = "!sh -c 'commit=$0 && branch=${1:-HEAD} && (git rev-list $commit..$branch –ancestry-path | cat -n; git rev-list $commit..$branch –first-parent | cat -n) | sort -k2 -s | uniq -f1 -d | sort -n | tail -1 | cut -f2'"
show-merge = "!sh -c 'merge=$(git find-merge $0 $1) && [ -n \"$merge\" ] && git show $merge'"
# pull and fast-forward ALL local branches
pull-all = !"for b in $(git for-each-ref refs/heads –format='%(refname)') ; do git checkout ${b#refs/heads/} ; git pull –ff-only ; done"
# list 10 most recent tags
tags-recent = !"git tag –list –sort=-refname |head -n 10"

view raw

.gitconfig

hosted with ❤ by GitHub

Make a comment