More productive with Git easily: create Aliases and Functions

When using the command line, we programmers end up typing certain commands very frequently. That’s where aliases come in handy. Today we’ll see how to easily become more productive with Git by creating custom aliases and functions. The following examples are made in Git Bash for Windows, but with small changes they apply to Unix systems as well.

Keep it short and quick with aliases

Probably the most common command when working with Git is git status. That command represents the compass that lets you know exactly where you are and the first step to take before any action.

Wouldn’t it be great if you could associate a custom shortcut to a command that you’ll use thousands of times a year? Luckily for us, that’s easier done than said.

To get a list of all available aliases, just open the Bash and type alias, the list will be printed on screen. Usually Git Bash for Windows comes with two built-in aliases, namely ll and ls. They are defined in the file <Git_folder>/etc/profile.d/aliases.sh, but we are not going to touch it. We’ll place our custom aliases in a new file.

First of all, go to your user home with the command cd ~.

We’ll store our custom alias in a file called .bash_profile. To open the file, or create a new one if you don’t have it yet, type nano .bash_profile and hit enter. The Nano text editor will open.

Append the following line to the file: alias gs='git status'. After that, save the file (press CTRL+O then enter to confirm file name) then exit (press CTRL+X).

.bash_profile file opened in nano editor

You can create or edit the .bash_profile file with any editor of your choice, i.e. Notepad++, Vim and so on. Any text editor will do. I’m just using Nano editor in this example because it comes with Git for Windows, so you are likely to have it. The important requirement is that the file is in your user home folder.

To make sure the newly defined alias is loaded, close the Git Bash window and open a new one. Now you can go to any folder on your machine that contains a Git repository (here I’m picking the project folder from the previous post), type gs and hit enter. You’ll see the exact same output as if you had typed git status.

git bash window showing the output of the command git status invoked via custom alias

No matter how fast you are at typing git status, you’ll type gs much faster. Besides, less characters to type means less chances of typos. It’s really worth it to create custom aliases.

What if i need parameters? define a function

Another Git command that I find myself using all the time is git log --oneline --graph --all --max-count=20. This command will display the commits’ log in the form of a graph, including all branches, with each commit compacted in one single line containing only the title and the first 7 characters of the commit hash. It is very useful when working with a fairly complex git repository that has several branches. But at the same time if the commit history is very long you may only want the latest given amount of commits. That is exactly what the option --max-count is for.

To make it short, if git status tells you where you are, git log --oneline --graph --all --max-count=20 tells you how you got there. As you can see the latter command is even longer than git status, so it makes up for an even better candidate for an alias. Anyway, unlike git status, this command has a numeric parameter, namely the amount of commits to display. We could make an alias for it, but then we would be forced to always show a fixed amount of 20 commits. What if I wanted to show a higher or lower amount of commits every time?

In this case we’re better off defining a custom function. Functions allow to pass parameters, hence we could decide each time how many commits we want to have displayed. We could even set a default value, so that we are not forced to provide the parameter every time we call the function. And that’s exactly what we are going to do.

The custom functions will go into the same .bash_profile file where we defined our custom alias. We want to keep names as short as possible, because speed is the main factor, hence the function name will be gg (acronym for “git graph”).

Just open the .bash_profile file with your favourite editor and append the following lines. It is advisable to leave a blank line between the alias and the function for better readability.

function gg() {
  git log --graph --all --oneline --max-count=${1:-20}
}

The function’s body is nothing but the git command itself, the only fancy thing is the final --max-count=${1:-20}. The ${1:-20} part simply means “insert the first function argument here, if no arg is available then use 20 as the default value”.

Testing our custom function

Now save the .bash_profile file and exit the editor. Close the Git Bash window and open it again. Now let’s cd into a folder containing a git project to test our brand new custom function.When we type gg and hit enter we see the entire commit graph, because our repo has only 18 commits, which is less than the default max count of 20.

git bash window showing the commits' graph after calling our custom function with default implicit arg

When we type gg 5 and hit enter we see only the last 5 commits from the graph. Of course we could also call the function with a value higher than the default, e.g. gg 30.

git bash window showing the commits' graph after calling our custom function with explicit non-default arg

Conclusion

We saw how, when working in bash, we can easily become more productive with Git by creating custom aliases and functions. They allow us to use short char sequences in lieu of long complex commands. Furthermore, they are easy to define and customize. In our examples we defined a custom alias and a custom function to call Git commands. Anyway, aliases and functions can be created for any command that would work in bash, not just Git commands.