Hello everyone! Today I decided to finally do something I postponed for a very long time, which is to set up my Git aliases.
I recently switched to a new laptop and OS, and I find the mental remapping of keys quite annoying. Consequently, my efficiency with the keyboard suffered quite a bit, and this resurfaced the idea of creating aliases for different CLI tools I use. So today, I start with Git.
What is an alias
If you have no idea what I'm talking about, you might find the following links helpful:
In short, an alias is a shorthand for a specific command. By using these shorthands, you avoid typing long commands, thus improving your efficiency and productivity.
At this point, you might be thinking, "Come on, the most heavily used Git commands are not that long. Why should I get into this hustle?" I can relate to that question, and I guess this is the reason I was postponing it for this long. However, I will argue that you get a productivity boost even for the short commands, mainly because you use them heavily. So if you are not convinced, I encourage you to follow along with this post and try creating them now.
If you can't relate to the question above and you are wondering how I was able to live that long without aliases, you might find this post dull. Nevertheless, you can keep reading and maybe post a tip or two in the comments. ;)
After doing a brief research on Google, I concluded that there are two ways of doing this:
- Create aliases in
- Create aliases in your shell configuration, which is
.bashrcfor my case.
However, we can also combine the two different ways mentioned above.
Creating aliases in .gitconfig
Let's start with the
.gitconfig option. You can also look at the documentation here.
.gitconfig, we can create shortcuts or new commands. For example, with the following command, we create a shorthand for
git config --global alias.a add
This will make the two commands below equivalent:
git add filename1.txt git a filename1.txt
We can also create new commands. For example, the command below will create the alias
unstage that will remove the file you specify from the changes you want to commit.
git config --global alias.unstage 'reset HEAD --'
Making the two commands below equivalent:
git unstage filename1.txt git reset HEAD -- filename1.txt
Creating aliases in Bash shell
To create aliases in Bash you use the alias command. You can declare your aliases directly in your
.bashrc like this:
alias gst='git status' alias ga='git add'
However, a more scalable way to add aliases is to create a separate
.bash_aliases file where you define all your aliases and then source this file from your
Below is an example of a
alias gst='git status' alias ga='git add'
You also need to add the snippet below in your
.bashrc to source the
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
So far, we merely presented two different ways of creating aliases. However, if we combine what we used above, we can unlock many more possibilities. Let's use the
git unstage command we previously created as an example.
.bash_aliases, we can also create a shortcut for the
.gitconfig aliases. Consequently, the following alias will trigger the
git unstage we created.
alias gun='git unstage'
Thus making the following two commands equivalent:
gun filename1.txt git reset HEAD -- filename1.txt
In my opinion, the best way to organize your shortcuts is to create custom commands using
.gitconfig and then use shell aliases for the shorthand creation.
However, a problem with this approach is that you lose auto-completion for branches. To fix this, first, you have to find where the script for the Git bash-completion is stored in your machine. Regarding that, I found this Stack Overflow question very helpful. In addition, you have to declare the Git completion for your aliases. For example, to add auto-completion in my
git checkout shortcut, I had to add the following in my
if [ -f /usr/share/bash-completion/completions/git ]; then source /usr/share/bash-completion/completions/git # Add git completion to aliases, make sure aliases exist __git_complete gco _git_checkout fi alias gco='git checkout'
My current setup
Since I spent most of my time researching and trying to figure the autocompletion issue, I didn't have a lot of time formulating my aliases in the scope of this post. So I just added the basics and will gradually improve it in order to adjust it to my everyday workflow.
However, I found this page in the Git wiki that seems like it has many pieces of gold.
Below are my
.bash_aliases, that I renamed to
.git_aliases.sh since it is tightly coupled to Git logic:
So that was for today, I hope you find the post informative! Do you use Git aliases? How do you structure your aliases? Do you have any cheatsheets for aliases? Let me know in the comment section below.