Useful Git Alias

August 19, 2016

Git is the de facto version control system at Committed Software and may have even influenced our name. Learning just a few commands can get you a long way but if you use it for a while you’ll find yourself using some of the commands more than others and maybe that some of the more useful options are difficult to remember. Thankfully, Git has a built in rich alias system to let you make your most used commands easier to remember and quicker to type. Here are some of our favourite aliases.

You can find all these aliases in this Gist along with a few other simple shortenings and the odd extra as I find them.

List aliases

This first alias is used to list all your aliases, helping you to remember them.

la = "!git config -l | grep alias | cut -c 7-"

Git’s alias normally interprets the command as a git command (effectively adding git to the start) but starting it with ! changes this behavior and runs the rest as a bash command allowing you to use bash scripting, piping and all the other bash commands like grep and cut in the above alias.

Listing

git log is very powerful and has many configuration options. These log aliases list the commits in increasing detail l, ll, lll.

l = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short

l

ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat

ll

lll = log -u

lll

Diff

A simple diff to show you what has changed since the last commit.

diffc = diff --cached HEAD^

Staging

The following set of aliases helps to quickly stage files for a commit.

First, to show a numbered list of modified files:

st = "!git status -s | cat -n"

Which gives:

$ git st
     1  ?? content/blog/201608/git-alias.md
     3  ?? static/img/blog/git/

Then you can stage and un-stage using the numbers:

s = "!stage() { git add `git st | sed -n $1p | awk -F' ' '{ print $2 }'`; git st; }; stage"
u = "!unstage() { git reset HEAD `git st | sed -n $1p | awk -F' ' '{ print $2 }'`; git st; }; unstage"

For example:

$ git s 1
    1  A  content/blog/201608/git-alias.md
    2  ?? static/img/blog/git/

$ git u 1
    1  ??  content/blog/201608/git-alias.md
    2  ?? static/img/blog/git/

Or you can just stage all changes with:

aa = !git add -u && git add . && git st

These show how you can define a function in the alias declaration and then call it. This allows you to reference the arguments passed to the command.

Quick commits

To make a commit with the supplied message e.g. git cm "Best work ever"

cm = "!cm() { git commit -m \"$1\"; }; cm"

Or to amend the last commit git ca

ca = commit --amend --no-edit

Branching

To create a new branch at the current commit, or move an existing branch, git mb new-branch

mb = checkout -B

To checkout a particular file from a different branch git cb other-branch /file/to/checkout

cb = "!cb() { git checkout $1 -- $2; }; cb"

Reseting

To reset one commit git re1, reset hard git reh or reset hard and clean to make sure git rec:

re1 = reset HEAD^
reh = reset --hard
rec = !git reh && git clean -fd

Rebase

To rebase interactively from a number of commits back git ri 5:

ri = "!ri() { git rebase -i HEAD~$1; }; ri"

Searching

As git’s alias allows you to redefine commands so you can add options as defaults if you find you always user them. This is used here to make searches include filenames and ignore case:

grep = grep -Ii

This alias is to search for files:

f = "!git ls-files | grep --color -i"

Bonuses

A useful alias if you are working on Windows to make scripts runnable on checkout:

addx = update-index --chmod=+x

If you use gerrit with the following aliases you can push for review git review myBranch or for drafts with git draft myBranch:

review = "!review() { git push origin HEAD:refs/for/$1; }; review"
draft = "!draft() { git push origin HEAD:refs/drafts/$1; }; draft"

A handy one to catch a common misstyping I make git k

k = !gitk

Finally a little alias to see how many commits have been made by different authors:

who = shortlog -n -s --no-merges

Credits

Many of these aliases came from or were inspired by this article http://durdn.com/blog/2012/11/22/must-have-git-aliases-advanced-examples/

Work with us

Have a project or an idea? We'd love to talk with you and see if we can help.

GET IN TOUCH
.

3 Strand Court

Bath Road

Cheltenham

Gloucestershire

GL53 7LW

© Committed