Thursday, May 17, 2012 10:18 AM
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:
Filed under: Code, Tips, Source Control, Git