Git Logo

Git Alias

What is an alias?

An alias is a short cut command to a longer command.

Why alias is required ?

With alias in place, a user can run complex commands with less typing and without the need to memorise. In short, alias brings faster and easier execution of complex commands.

Why this tutorial ?

Git, being the widely used SCM today, has lots of commands that lists useful information to the user but remembering too many flags/switches and formats is difficult. In order to use complex commands in easier way, this tutorial will help.

The Setup

  1. Open gitconfig by running the command
git config --global -e

2. If you do not have [format] or [aliases] already set, then copy paste the below content at the end of gitconfig file. Otherwise, add the content at appropriate section in the gitconfig file.

[format]
pretty
= ‘%C(yellow)[%H]%Creset committed by %C(green) [%cn]%Creset on %C(magenta)[%cd]%Creset with %C(cyan)[%s]%Creset’
[alias] hist = log --no-merges --decorate --date=short auth = “!f() { \ git log --no-merges --decorate --date=short --author=${1} ${@:2}; \ }; f” after = “!f() { \ git log --no-merges --decorate --date=short --after=${1} ${@:2}; \ }; f” before = “!f() { \ git log --no-merges --decorate --date=short --before=${1} ${@:2}; \ }; f” between = “!f() { \ git log --no-merges --decorate --date=short --after=${1} --before=${2} ${@:3}; \ }; f” src = “!f() { \ git log --no-merges ${@:2} --decorate --date=short — ${1}; \ }; f” cb = “!f() { \ git log --no-merges --decorate --date=short ${1}..${2}; \ }; f” cfb = “!f() { \ git diff ${1} ${2} ${3}; \ }; f”

3. Save and exit the config file.

[format] Section

This section is used to format the “git log” output. I have used “pretty” format, which adds pretty:format=”<pretty>” in all the “git log” commands.

I have used colour codes such as yellow, cyan, green and magenta. The following colour codes are supported

· black

· red

· green

· yellow

· blue

· magenta

· cyan

The colours should be mentioned within parenthesis (). For red, blue and green, parenthesis () is optional.

[alias] Section

The alias section helps to set aliases for any git command. I have used aliases for various “git log” & “git diff” commands in my gitconfig.

To explain all the aliases, I have cloned the following repository from github https://github.com/hhatto/gocloc . Now, let’s see each alias with a sample output.

Running this command, executes the command represented by the alias “hist” in [alias] section of the gitconfig file. This command lists the commit history, printing each commit in single line that has details such as SHA, author, date of commit and the comment used during commit.

git hist
Output: git hist

Running this command, executes the command represented by the alias “auth” in [alias] section of the gitconfig file. “git auth” takes author name as input and lists all the commits done by the given author name.

git auth "Hideo.Hattori"
Output: git auth “Hideo.Hattori”

The screenshot shows the commits of the author “Hideo Hattori

Running this command, executes the command represented by the alias “after” in [alias] section of the gitconfig file. The primary input for “git after” is the “date” in yyyy-mm-dd format (date format can be changed as required/desired). The command lists all the commits done after the given date.

git after "2020-03-01"
Output: git after “2020–03–01”

The screenshot shows the commits done after the date “2020–03–01”.

Running this command, executes the command represented by the alias “before” in [alias] section of the gitconfig file. The primary input for “git before” is the “date” in yyyy-mm-dd format (date format can be changed as required/desired). The command lists all the commits done before the given date.

git before "2020-03-01"
Output: git before “2020–03–01”

The screenshot shows the commits done before the date “2020–03–01”

Running this command, executes the command represented by the alias “between” in [alias] section of the gitconfig file. The primary input for “git between” is the “from date” & “to date” in yyyy-mm-dd format (date format can be changed as required/desired). The command lists all the commits done between two given dates.

git between "2020-02-01" "2020-03-01"
Output: git between “2020–02–01” “2020–03–01”

The screenshot shows the commits done between “2020–02–01” and “2020–03–01”

Running this command, executes the command represented by the alias “src” in [alias] section of the gitconfig file. The primary input for “git src” is the file name with path. The command lists all the commits done on the given file.

git src ./utils.go
Output: git src ./utils.go

The screenshot shows the commits done on the file ./utils.go.

Running this command, executes the command represented by the alias “cb” in [alias] section of the gitconfig file. “git cb” helps in comparing two branches, so the command takes two primary inputs, source branch & target branch. I have created a branch named “feature” and did two dummy commits. Now let’s see the output of “git cb

git cb master feature
Output: git cb master feature

The screenshot shows the commits difference in commits between master and feature branch.

Running this command, executes the command represented by the alias “cfb” in [alias] section of the gitconfig file. “git cfb” helps in comparing a file between two branches, so the command takes three primary inputs, source branch, target branch & the file with path. Now let’s compare the file “utils.go” between master and feature branches.

git cfb master feature ./utils.go
Output: git cfb master feature ./utils.go

Tips & Tricks

  1. All the aliases that is based on “git log” will accept the other switches/flags supported by “git log”. Listing down some examples.
git hist --stat 
Output: git hist — stat
git hist --numstat 
Output: git hist — numstat

2. The alias name can be changed as desired.

Conclusion

Git aliases are one of the cool features that git provides. One can write a big pretty git command and save it as a meaningful alias and run the alias N-number of times without having to memorise the full command.

The tutorial shares only the basic idea to begin with aliases in git. Users can explore and add much more.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store