November 30, 2019
Nood tidbits are a series of short posts that cover a range of things that I've discovered in my first few years of engineering that have been game changers for me. Each tidbit it something that I wish I wish I had known about when I first entered this industry.
As developers, we are forever finding ways to make things easier for ourselves: setting up our favorite text editors with our config files tweaked exactly how we like them, automated pipelines, and just about anything that allows us to do less typing. There are so many things I've discovered since I became a software engineer that I wish I knew when I started and one of them is the ability to add aliases to your local .gitconfig file. This provides you with the ability to give all the git commands you use in your terminal a shortened name so you don't have to type of each one every time you want to run it.
Let's start by making our way into our home directory. From anywhere in your terminal run
cd ~
. cd stands for change directory and the tilde is a shortcut for home. From here, start by running ls -la
which will allow you to see the full list of all files, including those that are hidden and can't be viewed from the GUI or by simply running ls
. Now you probably have a fairly long list of files in there so, feel free to look through around, there's probably some files you've never seen, or if you only want to look at git files only, run ls -la | grep git
. Adding | grep [keyword]
allows you to narrow down the search to make it easier to find anything you're looking for in your terminal.You should see a list of files similar to the screenshot above. The one we're after is the one at the top of the list: .gitconfig. If you simply want to view the contents you can run
cat .gitconfig
, which will print the contents of the file to the terminal, but, if you'd like to edit the file use your favorite text editor to open this file e.g. code .gitconfig
is the shortcut command for Visual Studio Code to open a single file. Alternatively, if you know your way around vim, run vim .gitconfig
.You'll notice that there are a number of sections in your .gitconfig file but for the purpose of this post, we are focusing on the section titled '[alias]'. Your alias section will look similar to the screenshot below, minus the commands that I have added myself.
From here, you can now add any git command you would like, preceded by a shortened name. You can see there are some commands in there that are just purely me being lazy: p for push, co for checkout, br for branch and so on but there are other, much longer commands where this really comes in handy. For instance, I run the command
git log
fairly frequently (particularly when at work) to look through the history of commits. Running that brings up something like this:There's nothing wrong with running
git log
as it gives me the information I'm looking for but, as you can see, each commit in the history takes up a decent chunk of screen real estate. As an alternative, I like to run the alias: git ll
which is defined as ll = log --graph --all --pretty=format:\"%Cred%h %>(11,trunc)%Cgreen%cr %<(10,trunc)%Cblue%cn%C(yellow)%d %Creset%s\"
in my .gitconfig file. There's no way on earth I will ever remember even half of that command but, by adding it to the alias section in my .gitconfig file, I don't have to worry about that. If you've never used this extremely long command before, feel free to give it a whirl, and maybe even add it to your own .gitconfig file. The output you'll see will be something similar to this (ignore my lazy, I'm-the-only-one-working-on-this-project commit messages):Happy alias creating! If you have a second, I'd love to know what your favorite git commands are. Feel free to share in the comments section below.
...