DEV Community

Cover image for Stop aliasing core Git commands
Jason McCreary
Jason McCreary

Posted on • Originally published at jason.pureconcepts.net

Stop aliasing core Git commands

A core feature of Git is the ability to create aliases. This effectively allows you to customize Git's command set. As a developer, of course, you're going to want to do this.

However, lately I've come across numerous claims stating aliasing core commands is the Right Way to use Git. Unfortunately, even Pro Git aliases core Git commands in their examples.

Regardless, this is not the Right Way.

Why? Two reasons: obfuscation and speed.

Obfuscation

While aliases give us freedom, there's no convention for aliasing core commands. So they're all subjective.

Sample aliases of core Git commands

While these commands exhibit our personal flare, they've lost their meaning. Sure git up sounds cool and might impress your coworkers. But they have no idea what it does and it isn't available on their setup.

Speed

The primary motivation for aliasing core commands is speed. Oh, the need for speed. Anything to save a few keystrokes. But how many keystrokes are you really saving by aliasing core Git comamnds?

Let's compare some common aliases against command completion.

Keystroke comparison between aliases and command completion

With the exception of git status, command completion tied or beat aliases. In addition, command completion also completes references and options. So command completion saves keystrokes across all commands, not just aliases.

In the end, aliases are a useful feature. But stop aliasing core Git commands. Instead, use command completion as a clearer and often faster alternative.

Reserve aliases for Git commands you run frequently and require options. For example, here are my current aliases. Two alias long git log commands and the others compliment Git's command set with additional custom commands.

My current Git aliases

Update
There seem to be confusion about scope. My suggestion is not to create Git aliases for core Git commands. This includes things like git st, git co, git ci as shorthands for git status, git checkout, git commit respectively. It does not include Git aliases in general or system aliases.

Want to master Git? Getting Git contains over 60 videos covering Git commands as well as scenarios you'll encounter using Git every day.

Top comments (41)

Collapse
 
smad1705 profile image
Damien Bouvy

I'm quite sorry, but the point of having my own personal development machine is to set it up the way I like :)

Sure git up sounds cool and might impress your coworkers. But they have no idea what it does and it isn't available on their setup.

So what? I'll tell them if they ask nicely :) I don't see how setting up shortcuts, aliases or magical key combinations is anyone's business but mine...

Unless it's on a common server of course, in which case I tend to agree.

Collapse
 
ryanwinchester profile image
Ryan Winchester

Okay, run your tests against my most used ones:

gst == git status
gca == git commit --all
gcam "message" == git commit -am "message"
gp == git push

:D I know what they stand for, don't care if others do, and don't care if they aren't available on a computer that's not mine.

Collapse
 
philnash profile image
Phil Nash

gst? That's far too many characters!

I have a bash function g that aliases git unless you pass no arguments, then it aliases git status.

Single characters FTW! :D

Collapse
 
ryanwinchester profile image
Ryan Winchester

haha! that's great xD

Collapse
 
littlefox profile image
Mara Sophie Grosch (LittleFox)

Kinda cool ^^

I have "g." for "git status ." but may switch to "g" :D

Collapse
 
loderunner profile image
Charles Francoise

You're catching a lot of "I do what I want" in the comments, but I fully support this idea. I expect everyone to be proficient with bare bones git, be able to share scripts, shell history and the likes.

As for those who don't want to type more characters, this two-liner will set you up for tab autocompletion with git... if you're on a Mac with brew installed. But there's probably a similar trick for Linux (or it might even be installed by default).

brew install bash-completion@2
ln -s /Applications/Xcode.app/Contents/Developer/usr/share/git-core/git-completion.bash /usr/local/etc/bash_completion.d/git
Enter fullscreen mode Exit fullscreen mode

You can even use tab completion for branch names now. ๐Ÿ™Œ

Collapse
 
senjai profile image
Richard Wilson

This article isn't stating "don't use git aliases in scripts" it's stating subjectively that completion is better for you.

Even if you alias everything under the sun, in scripts you want to share or work regardless of changes to aliases you can still use the original commands.

Collapse
 
gonedark profile image
Jason McCreary

Ha. That's a good way to put it. Thanks for reading the full article.

Collapse
 
carlesm profile image
Carles Mateu

I'm amazed no one has yet confessed to major sin:

backup = git add * && git commit -a -m "Done for today, cya tomorrow" && git push 
Enter fullscreen mode Exit fullscreen mode

:-D

Collapse
 
okayrene profile image
Renรฉ

So we shouldn't alias git commands because someone else might need to use my computer and not immediately know what my aliases are bound to?

Maybe you don't mean it that way, but all this comes off as needlessly puritanical.

Collapse
 
ns01 profile image
Eduardo Reyes

You also don't remove the original command when doing an alias, so if somebody were to sit in my computer (which no one does) he'd just have to type git status instead of gst or git s.

Collapse
 
diegolago profile image
Diego Lago

โ€œStop doing whatever you wantโ€ could be a good title for this post.

Git aliases are for PERSONAL use. Why shouldn't I do if I feel more confortable with those?

Collapse
 
sapling_nxt profile image
David

Why should I care if others can't use my git configuration un my machine. aliases should make working more comfortable for me not for others. If i want to explain how they work i can introduce them to my set of alias or use the default ones.

Collapse
 
ns01 profile image
Eduardo Reyes

Under the same logic we should all use the same editor with no configurations, since "somebody could use my computer at some point" the article is useless.

Collapse
 
bgadrian profile image
Adrian B.G. • Edited

I know there are some reasons why not to use them, but these 2 are just not valid, you are contradicting yourself.

they've lost their meaning. Sure git up sounds cool and might impress your coworkers. But they have no idea what it does and it isn't available on their setup.

"git st" has the same meaning as the "status", because they are synomyms--aliases.
What should the coworkers have to do with my setup?

speed

git com+tab contains more keys strokes than "git ci".

What's next, do not setup custom key bindings to my IDE? ๐Ÿ˜ฑ (both your reasons can be applied to custom key bindings). It doesn't improve speed (press the same no of keys) and obfuscate (is not the same as default value).

Collapse
 
gonedark profile image
Jason McCreary

You're right, I don't setup custom key bindings in my IDE. ๐Ÿ˜‚

Collapse
 
ktec profile image
globalkeith

I've aliased git add . && git commit -m wip to save_game which makes me smile whenever I use it. You'll need to understand rebase to clean up after tho.

Collapse
 
senjai profile image
Richard Wilson

What even?

Why would you make the case to tell people to stop customizing their environment to their liking?

It's not like an alias removes the original command.

Gws, gcf, gca, glg, gri all mean something to me that many might not follow but they improve my workflow.

If someone sits at my machine they can still use normal git commands.

I would take the stance that everyone should experiment with what works for them and opt to stray from the beaten path in their working environment. Everyone has their own approach so this article is entirely subjective.