DEV Community

Committed Software
Committed Software

Posted on • Originally published at committed.software on

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/

Top comments (7)

Collapse
 
skittishsloth profile image
Matthew Cory

My bash (and git) skills are lacking, so this may be obvious, but why are you declaring functions in the aliases just to immediately call them? Is it not possible to have the full commands as an alias without the function wrapper? Good stuff though :)

Collapse
 
committedsw profile image
Committed Software • Edited

Basically, you need to do that when you want to work with the arguments. Otherwise, all the arguments go to whatever the last command in the alias is.

Collapse
 
skittishsloth profile image
Matthew Cory

Ah, okay - that makes sense. Thanks for clearing that up!

Collapse
 
computersmiths profile image
ComputerSmiths

Just curious: (I don't 'git', shows you what level Dev I am...) Does having aliases ever trip you up, especially if they aren't standard across developers or platforms? If I've got 'k' aliased to "kill all processes starting with the string " and you are used to !gitk and you sit down at my terminal to help me, Wierd And Unexpected Things are going to happen...

My experience with aliases is that it's hard to keep them in sync across many machines, and if something doesn't work, I spend more time looking up what I expect the alias to do than I saved by making it universal (for me). Half the time I have to remember which machine(s) it works on and ssh over there to show me the alias, not a time-saving maneuver...

If it's company-wide and everyone knows what to expect, then it's probably OK, but "I wish this was a command" always strikes me as fraught with peril.

Collapse
 
committedsw profile image
Committed Software

That’s a fair question and something that does put people off using them. It can trip me up, but it doesn’t happen much. I have my aliases on a gist (gist.github.com/stuarthendren/7a3b...) so if I change machine, or go to the cloud, I can quickly set them up again. If I’m on someone else machine I would try to avoid making commits and getting them into trouble. I also wouldn’t recommend using aliases until you’re confident with git, that way you aren’t reliant on the aliases and should still know the underlying commands.

Collapse
 
fniessen profile image
Fabrice Niessen

Hello! The idea of being able to use numbers to stage/unstage files, for example, is very interesting. What a pitty that cat does loose all the colors we get, by default, in the git status output…

Any idea how to get both advantages?

Collapse
 
committedsw profile image
Committed Software

I don't unfortunately, but I'll take a look into it and see if it's possible.