Using aliases to simplify working with git

Anthony Musyoki
The Andela Way
Published in
3 min readJan 6, 2020

--

Photo by Marc-Olivier Jodoin on Unsplash

Git is the most commonly used version control system (VCS). As a distributed VCS, every developer’s local copy of the codebase is a full copy of the data. This is unlike centralized VCS where a central server holds full copies of the data.

Most developers prefer working with git commands on a terminal whereas a few others will use their IDE’s git integration to perform common git actions. Using git commands on a terminal enables one to acquire a deeper understanding of git as opposed to clicking around in an IDE.

Larry Wall, the creator of Perl said that the first great virtue of a programmer is laziness. This is not a resistance-to-work kind of laziness, but a desire to expend minimum energy or a desire to create and use tools that simplify our daily work.

If you are a software developer using git on the terminal, you will probably type common git commands on your keyboard thousands of times in your lifetime. The good kind of laziness will move you to look for ways to simplify this.

Enter git aliases.

Git Aliases

Git ships with the ability to create aliases. Using global configs, you can easily and quickly create an alias for a git command.

Imagine a scenario where you want to quickly see all your commits in the current branch.

The full command is

git log — author=my_git_username

A git alias can be created to reduce the entire command to

git mine

This is achieved by adding a git alias by running the following:

git config — global alias.mine ‘!git log — author=”$(git config user.name)”’

As a developer you can change mineto a word of your choice.

Another common case is when working on open source software using git-workflow. You will periodically be required to update your current working branch with the latest upstream changes.

The steps would probably be:

git stash
git fetch origin
git merge — rebase origin/master
git stash pop

This can be reduced to git update by running:

git config — global alias.update ‘!git stash && git fetch origin && git merge — rebase origin/master && git stash pop’

For a workflow where you create a new feature branch from origin/dev every time you are working on a new feature you will probably run:

git fetch origin
git checkout -b feature_branch origin/dev

This can be again be reduced to git start feature_branch by adding the start alias vis;

git config — global alias.start ‘!git fetch origin && git checkout -b $1 origin/master && :’

You have probably noticed that all git aliases have to start with git.

Enter bash aliases.

Bash Aliases

Bash aliases are not limited in their usage like git aliases. Bash aliases are usually defined in ~/.bash_alias or ~/.bashrc

Bash aliases can be created for the most common git commands by adding them to the aforementioned files.

Add the following lines to your file

alias ga='git add'
alias gac='git add . && git commit -a -m'
alias gd='git diff'
alias gco='git checkout'
alias gpo='git push origin “$(git_current_branch)”'
alias gpu='git push upstream “$(git_current_branch)”'
alias gup='git stash && git pull — rebase origin/master && git stash pop'

You can now simply type ga README.md instead of the full git add README.md

A word of caution is in order; Ensure that you understand what a git command does before creating its alias. Aliases also need to be updated as git evolves. E.g git 2.23 split out the usage of git checkout into two new subcommands git switch and git restore.

Once you are familiar with using your defined git aliases you can super-charge your workflow by using the git aliases defined in the oh-my-zsh framework. Setting up and using oh-my-zsh is a story for another day.

Further reading:

--

--