git

Useful Git Alias

committedsw

Committed Software

Posted on October 13, 2018

Useful Git Alias

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-"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

l

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

ll

lll = log -u
Enter fullscreen mode Exit fullscreen mode

lll

Diff

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

diffc = diff --cached HEAD^
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Which gives:

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

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"
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

Or you can just stage all changes with:

aa = !git add -u && git add . && git st
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Or to amend the last commit git ca

ca = commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

Branching

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

mb = checkout -B
Enter fullscreen mode Exit fullscreen mode

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

cb = "!cb() { git checkout $1 -- $2; }; cb"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Rebase

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

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

Searching

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

grep = grep -Ii
Enter fullscreen mode Exit fullscreen mode

This alias is to search for files:

f = "!git ls-files | grep --color -i"
Enter fullscreen mode Exit fullscreen mode

Bonuses

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

addx = update-index --chmod=+x
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

k = !gitk
Enter fullscreen mode Exit fullscreen mode

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

who = shortlog -n -s --no-merges
Enter fullscreen mode Exit fullscreen mode

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/

💖 💪 🙅 🚩
committedsw
Committed Software

Posted on October 13, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related