I'm using git quite often so I've started to use a bunch of git aliases, now I will share them with you :)


First install hub what is a great extension of git and provide a lot of GitHub shorcuts. So just write

$ curl http://defunkt.io/hub/standalone -sLo ~/bin/hub
$ chmod +x ~/bin/hub

or if you are using MacOS you can use brew

$ brew install hub

Then check if it's working

$ hub version
git version 1.7.9
hub version 1.8.4 # ← it works!

After that we do some magic (I assume that you are using ZSH, if other, you need to write your proper RC file):

$ echo 'eval $(hub alias -s)' >> ~/.zshrc
$ echo 'alias g=git' >> ~/.zshrc

Now i.e. when we want to clone repo from GitHub we only need to write:

$ g clone some-our-repo

And if it's someone other repo then we have (in this example I use my sass-960gs repo):

$ g clone hauleth/sass-960gs

More of hub utilities is on it's homepage.

Start aliasing

So when we have our awesome hub and g alias we need to shortcut more git commands, so begin with very basic commands:

g config --global alias.ci commit
g config --global alias.cl clone
g config --global alias.co checkout
g config --global alias.f fetch
g config --global alias.b branch
g config --global alias.cp cherry-pick
g config --global alias.com 'checkout master'

Great, now we save 5-8 key strokes in most popular tasks. So let's go further:

g config --global color.status.added green
g config --global color.status.modified red
g config --global color.status.untracked blue
g config --global alias.st statis -sb

Now we have pretty short and pretty clear status output in your repo. All files in stage are green, unstaged but modified are red and all untracked files are blue what is pretty clear when you need to check what happening in your tree.

More git magic

It was only basis. Now we will setup some useful hacks for git masters.

Prettier logs :)

Now one of my favourite and widely described in Protips on Coderwall:

prettier version of git log

g config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Whoops, I forgot to add some files

A lot of times we accidentally forgot to add some files to our commit. Reverting it only for adding some minor edit in README or Procfile isn't very pretty. So what can we do? Use git commit --amend --reuse-message=HEAD. But it is too long and too ugly to use. So just add aliases to it:

g config --global alias.ca 'commit --amend --reuse-message=HEAD'

Yay. Pretty clear and pretty short. But remember that if you already pushed your commits to remote it will be bad idea to use this.

Clean untracked files

Sometimes we have a bunch of untracked files in our repo and also most of them is unneeded in our work (i.e. .gem files with builded RubyGem). So how to easily get rid of them in a second? Just make this alias:

g config --global alias.cleanse '!git ls-files --others --exclude-standard | xargs rm'

Now calling g cleanse will remove all untracked files from working tree.

Find conflicts

Sometimes when we merge some branches the tree-way merge cannot fix all of conflicts in files. Then we need to fix them using some merge tool or if someone is a purist then fix it by hand. For the second ones this alias will be very helpful:

g config --global alias.conflicts '!git ls-files -u | cut -f 2 | sort -u'

Quickly go to repo root

So why not use g root?

g config --global alias.root '!cd $(git rev-parse --show-toplevel)'

Some tasks to do? Check it out

According to bbatsov's Ruby Style annotations guide we have two most important annotations - TODO: and FIXME:. So lets find them in our tree.

g config --global alias.todo=grep --heading --break --ignore-case -e 'TODO:'
g config --global alias.fix=grep --heading --break --ignore-case -e 'FIX:' -e 'FIXME:'

Pretty clear, isn't it?