DEV Community

Shawn (He/Him)
Shawn (He/Him)

Posted on

Saving precious key presses with Git Aliases

Git is probably one of the more powerful tools that a developer will use through the entirity of their career(s).

But many tend to give up the power of the command line interface due to not wanting to type out their commands... every... single... time.

Unimpressed Ryan Reynolds

That's where the power of git aliases come in.

Don't worry you don't need to read the entire manual, I'll break it down simply for you.

It's an alias of a git command or set of commands

Standing Ovation

But seriously, in the long run aliases can save you those precious key strokes and help you get a little more comfortable in the command line.


How do I create an alias?

There's multiple ways to go about doing it, no way is better or worse than the other.

If you're comfortable in a terminal I recommend getting used to the command line ,however, if you don't want to spend time writing out git config --global alias. you can just edit the global .gitconfig.

It's best you find and get familiar with where your global .gitconfig file is on your machine.

Unix typicaly resides inside the ~home/ directory.

Windows will typically reside in your C:\<username> directory.

Individually (the hard way)

You can use the git command line interface to push an alias to your global config as follows.

➜  git-tutorial git:(master) git config --global alias.co checkout
Enter fullscreen mode Exit fullscreen mode

If you were to open your global .gitconfig file now you will notice an [alias] section containing something along the lines of:

[alias]
    co = checkout
Enter fullscreen mode Exit fullscreen mode

Editing your .gitconfig file (The easy way)

If you created an alias from the hard way section your .gitconfig will already contain an [alias] area, if you skipped that you will need to add it in.

[alias]
    co = checkout
Enter fullscreen mode Exit fullscreen mode

The variable is name of our alias that it wants to equal.

In this example, co is the same as checkout.

Simple saving our .gitconfig file our alias(es) will immediately be available to us in our terminal.

➜  git-tutorial git:(master) git co
fatal: You are on a branch yet to be born
Enter fullscreen mode Exit fullscreen mode

Powerful aliases

Each person is going to be different so aliases will vary.

Personally I use them to not type long words as I am prone to typing before my brain fully tells my fingers where to go on the keyboard.

Here are some of mine:

[alias]
    b = branch
    co = checkout
    s = status
    br = branch
    ci = commit
    st = status
Enter fullscreen mode Exit fullscreen mode

You'll notice that I can have multiple aliases for the same command. You can even have an alias call another alias.

Your aliases even have access to the flags of the thing they are referring to.

Knowing that we can build some fun alias commands:

    lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
    lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
    lg = !"git lg1"
Enter fullscreen mode Exit fullscreen mode

Imagine having to type all that out... everytime. Now we can just type git lg1 or git lg to call a big git command.

Take note that lg doing !"git <command>" or !"git lg1". The ! is a character used to espress 'run the following command(s) to the shell'.

TL;DR: git lg is just telling the shell to run git lg1.

Fast Typing

That's the beginnings of git aliases. If there's something you wish you could do shorten, git aliases are the way.

But just be careful... if you tell it do something like push --force in an alias... that's on you.

➜  git-tutorial git:(master) git config --global alias.yolo "push --force"
Enter fullscreen mode Exit fullscreen mode

Do you already use aliases? If so, what are some of your favorites?

Top comments (0)