Git Alias Configuration

Learn to create aliases in Git.

What is an alias?

If a command is very long, we can set an alias command for it. We do this by setting the alias in the config file in the .git folder.

There are several places where we can put the config file. Most likely, we’ll want to create this file in our home directory. That applies to all terminals on our machine and projects under our user account.

For example, here’s an alias to show all branches with one-line log commits and graph view:

[alias]
    st = status
    ll = log --all --oneline --graph --decorate --color

There are two methods by which we can set an alias. Each method is explained below.

First method

  1. In the main SampleFolder, run the ls -a command to see all the hidden files and folders.

  2. We see a folder named .git. Move into that folder using the cd .git command.

  3. Use the ls -a command again to see all files.

  4. Write the lines mentioned above in the config file using any editor or echo command:

    $ ls -a
    $ cd .git
    $ ls -a
    $ echo [alias] >> config
    $ echo st = status >> config
    $ echo ll = log --all --oneline --graph --decorate --color >> config
    
  5. Next, come back to the main folder using the cd . command.

  6. In the SampleProject folder, run the commands below to confirm the results:

    $ git status
    $ git st
    $ git log --all --oneline --graph --decorate --color
    $ git ll
    

The output of the first two and the last two commands should be consistent. If an alias isn’t made, it gives an error that no such command exists in Git.

Get hands-on with 1400+ tech skills courses.