Creating Git Aliases
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:
- https://www.atlassian.com/git/tutorials/git-alias
- https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
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. ;)
Creating aliases
After doing a brief research on Google, I concluded that there are two ways of doing this:
- Create aliases in
.gitconfig
. - Create aliases in your shell configuration, which is
.bashrc
for 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.
Using .gitconfig
, we can create shortcuts or new commands. For example, with the following command, we create a shorthand for git add
:
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 .bashrc
.
Below is an example of a .bash_aliases
file:
alias gst='git status'
alias ga='git add'
You also need to add the snippet below in your .bashrc
to source the .bash_aliases
file:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Combining forces
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.
In the .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 .bash_aliases
:
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 out 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 .gitconfig
and .bash_aliases
, that I renamed to .git_aliases.sh
since it is tightly coupled to Git logic:
[alias]
last = log -1 HEAD
amend = commit --amend -m
unstage = reset HEAD --
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
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 g __git_main
__git_complete ga _git_add
__git_complete gb _git_branch
__git_complete gco _git_checkout
__git_complete grb _git_rebase
__git_complete gf _git_fetch
__git_complete gp _git_push
fi
alias gc='git commit'
alias gst='git status'
# Require autocomplete
alias g='git'
alias ga='git add'
alias gb='git branch'
alias gco='git checkout'
alias grb='git rebase'
alias gf='git fetch'
alias gp='git push'
# Custom Comamands
alias gun='git unstage'
alias gam='git amend'
Summary
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 cheat sheets for aliases? Let me know in the comment section below.
Stay tuned!