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 alias.co checkout git config --global alias.st 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:
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:
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:
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'
[…] one of the previous posts at here, we discussed how we can use git aliases to improve the git experience. However, the […]
LikeLike