Make git experience smoother using git aliases

Working with git can be a little intimidating for one since it requires a steep learning curve. Aliases are one of the ways to make git experience more familiar, simpler and easier. It is not necessary that one know them but then can often come handy. Also, you can probably save yourself some time if you also set aliases for long commands. In this short post, we’ll learn on how to use git aliases.

Before we dive into aliases, let’s review the configuration scope in git. Git has three scopes for configuration:

System – Applied to all users in your computer.
Global – Applied to you only, but all repositories.
Local – Applied to the repository only.

Narrower scope overrides the value from wider scope. Therefore, local scope wins all the time. If you don’t specify scope to ‘git config’ command, git takes –local as default option.

Git doesn’t automatically understand or try to execute your command if you type it in partially. If you do not want to type the entire command, you can easily setup an alias for it using git config. Here are a couple of examples:

git config --global checkout
git config --global status

where the –global specifies the global configuration scope. The part after alias. specifies the alias text. So, first command means that instead of typing git commit, you just need to type git ci. Similarly, second command means instead of typing git status, you just need to to type git st:

git alias example 01

This technique can also be very useful in creating commands that you think should exist. For example, to view git logs in one line in pretty print, you can add an alias like below:

git config --global alias.ptlog "log --pretty=oneline --graph"

You’ll need to use single-quotes if you are using git on linux and double quotes if you are on the windows version of git.

This makes the following two commands equivalent:

git log --pretty=oneline --graph
git ptlog

This seems a bit clearer. It’s also common to add a last command, like this:

git config --global alias.last 'log -1 HEAD'

This way, you can see the last commit easily:

git alias example 02

It should be clear probably, that git simply replaces the new command with whatever you alias it for.

You can also view all your git aliases by using git config -l or by using git config –list:

viewing git aliases

There are also times, maybe you want to run an external command, rather than a git specific subcommand. In that case, you start the command with a ! character. This is useful if you write your own tools that work with a Git repository. We can demonstrate by aliasing git visual to run gitk:

git config --global alias.visual '!gitk'

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s