Git Aliases

Many git users may not be aware that you can setup git aliases for either providing shortcuts to existing commands (like typing ‘git co’ instead of ‘git checkout’) or even for creating entirely new commands (‘git hist’ for printing your git history formatted how you like it).  Git aliases can be local to a single repository, or (more commonly) global.

To manage git aliases you need to use the ‘git config’ command (see http://git-scm.com/docs/git-config), and for making your aliases global use ‘git config –global’.  Let’s make a quick alias now:

git config --global alias.co 'checkout'

This command tells git to create a global alias ‘co’ which will fire off the ‘checkout’ command when typed.  This saves a few keystrokes, so now ‘git co master’ does a checkout of master, or ‘git co mybranch’ will checkout the branch ‘mybranch’.

My Favorite Aliases

Here’s a list of my favorite aliases that I make sure to add to any new git install.

git config --global alias.go 'checkout'
git config --global alias.in 'commit -a'
git config --global alias.up 'push origin'
git config --global alias.down 'pull origin'

I particularly like these aliases since they reduce typing on the most common commands, but also because they make me feel just a little bit like James Brown singing:

uh uh git down! git in!  huh, yea.  git up!  ooohhh owwwww, git in now, yea git up.

Delusional rock fantasy aside, the above is actually a regular git flow I  use every day, using the above aliases to pull down the latest, check in changes, then push back up to origin.

Other Useful Aliases

Here’s a couple of other useful aliases to save you some finger and brain strain:

git config --global alias.st 'status'
git config --global alias.br 'checkout -b' //For creating branches
git config --global alias.a 'add .' //Shortcut to add all changes to the index
git config --global alias.hist = 'log --pretty --graph --date=short'

You can get pretty complex with aliases, having them call out to shell scripts or running multiple commands, but I’ll stick with the basics here & point you at some advanced usage examples on other pages at the end of this post.

Viewing Your Stored Aliases

Your aliases are stored in two places: global aliases are stored in ~/.gitconfig (.gitconfig in your home directory) , and local aliases are stored in .git/config (the config file within your local git directory).  However, you don’t need to ever look in those files—instead use one of the two the following commands:

git config --get-regex alias //Shows all of your aliases
git config --global --get-regex alias //Just global aliases

Now that you have a list of your aliases, you can add more or remove/replace using ‘git config’ (for example, to remove alias.test, use ‘git config –unset alias.test’)

Conclusion and Links

Git aliases can save you lots of typing and can prevent dangerous typos of difficult commands (for example, you could alias rebases or fetch/merge).  They are incredibly flexible, powerful, and easy to setup (and, since they live in your .gitconfig, quite portable). 

I’ll leave you with some links to useful git alias information:

Enjoy!

7 Comments

  • I'm amazed, I must say. Seldom do I come across a blog that's
    equally educative and amusing, and without a doubt, you've hit the nail on the head. The issue is something which too few men and women are speaking intelligently about. Now i'm
    very happy I found this during my hunt for something relating to
    this.

  • Nice blog here! Also your site loads up fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my site loaded up as fast as yours lolLeverage
    Season 5 Episode 15

  • I've been browsing online more than 3 hours lately, but I by no means found any interesting article like yours. It's pretty price enough for me.
    In my view, if all website owners and bloggers made
    excellent content material as you did, the web will likely be much more helpful than ever before.
    Downton Abbey Season 3 Episode 0 Series 3 Christmas Special

  • I am extremely inspired together with your writing talents and also with the format
    in your weblog. Is that this a paid theme or did you modify it your
    self? Either way keep up the nice quality writing, it is
    uncommon to look a great blog like this one these
    days..Watch The Clone Wars Season 5 Episode 12

  • Great stuff from you, man. Ive read your stuff before and youre just
    too awesome. I love what youve got here, love what youre saying and that the way you say it.
    You make it entertaining and you still have the capacity to keep it smart.
    I cant wait to read more out of you. This is actually
    a great blog.

  • Pretty! This has been an incredibly wonderful post. Many thanks for supplying this information.

  • Thanks very nice blog!

Comments have been disabled for this content.