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.
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:
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:
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:
- Git config man page
- Git wiki alias page on kernel.org—useful, detailed and complex examples
- StackExchange post on favorite git aliases (including one that goes to the ‘Danger Zone’)