This post is part of series of posts related to lesser known Git features, please visit the main post to check the series

If you like to use Git Bash to interact with Git rather than the client tool, then you will find it incredibly useful.

Simple Alias

Git allows you to create local aliases for the standard commands so you can for example, create a shorthand version of commands that you use the most

This command will create alias “cln” for Git command “clone”

$ git config --global alias.cln cloneKedar@MyMachine MINPQ55 ~
$ git config –global alias.cln cloneKedar@MyMachine MINPQ55 ~$ git cln https://github.com/kedarrkulkarni/git-playground.git
Cloning into ‘git-playground’…
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

Full Command Alias

The Git aliases can be expanded even further to create commands specific to your repository

Kedar@MyMachine MINPQ55 ~
$ cd git-playgroundKedar@MyMachine MINPQ55 ~/git-playground (master)
$ echo “test content for dummy file” > index.htmlKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git add .
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directoryKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git commit -m “commit message for dummy file”
[master 2f10acb] commit message for dummy file
1 file changed, 1 insertion(+)
create mode 100644 index.htmlKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git config –global alias.undo ‘reset –hard HEAD^’Kedar@MyMachine MINPQ55 ~/git-playground (master)
$ git log
commit 2f10acb025bfc7595442df333bf058ccae67c8db (HEAD -> master)
Author: Kedar <kedarrkulkarni@gmail.com>
Date: Sun Mar 1 15:21:47 2020 +0000commit message for dummy filecommit 3c69cffb9fca13943220dda8c6ae9d776c47d4c8 (origin/master, origin/HEAD)
Author: kedarrkulkarni <58665635+kedarrkulkarni@users.noreply.github.com>
Date: Sun Mar 1 12:25:43 2020 +0000Initial commitKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git undo
HEAD is now at 3c69cff Initial commitKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git log
commit 3c69cffb9fca13943220dda8c6ae9d776c47d4c8 (HEAD -> master, origin/master, origin/HEAD)
Author: kedarrkulkarni <58665635+kedarrkulkarni@users.noreply.github.com>
Date: Sun Mar 1 12:25:43 2020 +0000Initial commitKedar@MyMachine MINPQ55 ~/git-playground (master)
$

In above example, we have created alias “undo” for what otherwise be following command. Instead, you can simply use “git undo” command with the same effect.
$ git reset --hard HEAD^

Alias For Sequence Of Commands

In above example, we did hard reset the HEAD of the branch to discard all local commits.

Normally, when I undo the commits, I would like to see the “Git log” to see what’s the last commit I am working with.

You can simply run following two commands in order to do this
$ git undo
$ git log

But, how about creating alias that sequence of commands for us?

To do this, we must use “!” character as a prefix to the command chain while creating the alias

Kedar@MyMachine MINPQ55 ~/git-playground (master)
$ git config –global alias.undo-log ‘!git undo && git log && :’Kedar@MyMachine MINPQ55 ~/git-playground (master)
$ echo “test content for dummy file” > index.htmlKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git add .
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directoryKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git commit -m “commit message for dummy file”
[master 17455da] commit message for dummy file
1 file changed, 1 insertion(+)
create mode 100644 index.htmlKedar@MyMachine MINPQ55 ~/git-playground (master)
$ git undo-log
HEAD is now at 3c69cff Initial commit
commit 3c69cffb9fca13943220dda8c6ae9d776c47d4c8 (HEAD -> master, origin/master, origin/HEAD)
Author: kedarrkulkarni <58665635+kedarrkulkarni@users.noreply.github.com>
Date: Sun Mar 1 12:25:43 2020 +0000Initial commit

All the aliases created by you are stored in the “.gitconfig” file in the user profile directory, “/etc/gitconfig” file on Linux machine  or “~/.gitconfig” on Windows machine.

Gitconfig file provides more handy options, we will visit them in a separate post. Hope you find the aliases useful while using Git.

It would be great to know other innovative ways in which you use Git aliases, please comment below with your own example 🙂