Git Alias

In git and 10xJanuary 29, 20222 min read

Git is an amazing tool.

As developers, we take it for granted, but it is not the standard everywhere. It still amazes me that such feature doesn’t exist yet, eg: text editors, design tools, …

I might write more about Git, for now, let's focus on this cool feature called git alias.

Alias

Bill Gates said it himself:

I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.

So why should we keep writing long commands when they can be simplified? What if we can create an alias for those commands?

Sometimes it is about saving time, and sometimes about not having to remember every single combination of commands.

Let's see some examples:

  • git aa: To add all changes
  • git cm “<message>”: To commit added changes with a message
  • git cam “<message>”: To commit all changes with a message
  • git s: To show a summary of changes
  • git pu: Well we all hate git reminding us that first pushes to a branch require certain love git push –set-upstream origin <branch_name>
  • git b: Shows us all branches but sorted by last modified :)

So how can we add this superpower to our routine? Very easy. We just need to edit one file :)
Open in your fav. Editor ~/.git/config. Scroll through the file until you find a section called [alias].

Then we just need to add them there like:

[alias]
    aa = add *
    cm = commit -m
    cam = commit -a -m
    s = status
    pu = "!git push --set-upstream origin $(git branch --show-current)"
    b = "!git for-each-ref --sort='-authordate' --format='%(authordate)%09%(objectname:short)%09%(refname)' refs/heads | sed -e 's-refs/heads/--'"

You can find my current configuration here.

And for the full documentation, official docs are always the best resource.

Pd. If you like extremes, take a look at this.

Pd.2 If you are wondering about the bang before the last two aliases, check this.