DEV Community

Cover image for Stop aliasing core Git commands

Stop aliasing core Git commands

Jason McCreary on March 20, 2017

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'...
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
 
eduardort 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
 
eduardort 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.

Collapse
 
geoff profile image
Geoff Davis

I've messed around with Bash, and created a specific "Git shortcuts" bash file that essentially omits the "git" command.

This won't work in all setups, but it allows me to shorten commands while retaining readability, and tab-completion works still because it's used as a CLI:

status => git status
log => git log
branch -c someBranch => git branch someBranch && git checkout someBranch
clone -p someGitURI => git clone $(pbpaste)
pushup remoteName workingBranch => git push -u remoteName workingBranch

etc. Wondering what everyone's take on this method is.

Collapse
 
alexslynko profile image
Alex Slynko

You can use git checkout -b to create new branch and checkout to it.

Collapse
 
cmilr profile image
Cary Miller

My only issue with your post is that in the end, you didn't really show a compelling reason not to use aliases. Is there something you left out, maybe?

Collapse
 
meredevelopment profile image
Mere Development

Hey Jason, thanks for writing the article and doing the tests with characters used etc. I'll bear it in mind, especially when it comes to working in shared environments and helping others to become familiar with git. I'd always assumed that adding some short aliases did save time, and muscle memory will probably mean I do keep using them, but thanks nonetheless.

I've not written anything for dev.to yet, so not sure what sort of article editing controls there are, but if it's possible... how about changing the title to a 'suggestion' rather than a command, and completely avoid using any reference to the right way? I think you've accidentally undermined yourself by appearing to be dogmatic, which is a shame.

Collapse
 
gonedark profile image
Jason McCreary

Aliases are a point of customization and as such personal. I think this has caused some readers to react to keywords like right way and miss the focus of this post. While the title was indeed for effect, the post, when read closely, presents the argument for being clear while also providing an alternative.

Collapse
 
marcoslhc profile image
Marcos Hernández

There is no "right way" when designing your personal workflow. Who cares about "obfuscation" when is my terminal? and speed? really? sometimes having a good mnemonic is more efficient than saving characters

Collapse
 
_javierperez profile image
Javier Pérez Archila

Have you ever seen the Oh my Zsh aliases? gco, gl, gp, glgg, etc... they're great, I find this post valid for a CI server or some common machine running git, otherwise I agree that my computer my rules, personally I find the aliases very handy.

Collapse
 
ribugent profile image
Gerard Ribugent Navarro

I think aliases are a personal choice, someone prefers to use it, and someone prefers to use tab completion.

When I switched from subversion to git, at the beginning I missed the short commands, but finally I started using tab completion.

As author said, aliases are indisposable to run commands with a specific options.

For my daily work, I don't use aliases, but I use "custom" commands for my daily workflow:

  • git branch-create $JIRA_ISSUE creates new branch from the right branch with the right name according our rules
  • git branch-sync <--rebase> Merges or rebases branch from the base branch(it's deducted by the branch name)
  • git pr simple ncurses interface to open a pull requests, you don't need to specify the base branch again
  • git branch-clean <--remote> Cleans local/remote branches which has been merged into master

We have more, but I think these ones may be interesting :D

Collapse
 
richburroughs profile image
Rich Burroughs

Yeah I agree with a lot of the responses I'm seeing. Why should it matter to my coworkers what I do on my laptop? If we're pairing and there's something they don't understand, I can always explain it. And they probably are going to be able to guess what "git co" and "git st" mean, especially if they see the output. As long as I'm not putting the short versions in documentation, I don't see a problem. You could probably extrapolate this to a lot of shell aliases too and I'd have the same opinion.

Also I've been typing "git st" and "got co" for years now. It would not make me more efficient to remove those and have to tab complete them. I've been in shells without my aliases and believe me, it does not save me keystrokes.

Collapse
 
jayzces profile image
Louise Hermosa

Sorry, but I don't use aliases to impress anybody. I just like having shortcuts. And it definitely minimizes the chances of errors. A two character command is easier to get right than say a 12 character command.

Collapse
 
streeetlamp profile image
streetlamp

sorry but i'll setup my personal aliases however i like? i dont care what aliases my coworkers use for the same tasks and im sure they dont care what i use. its our own systems setup how the way we like and are comfortable with. im totally baffled by this article to be honest.

Collapse
 
pedromanoel profile image
Pedro Manoel Fabiano Alves Evangelista

I really like my git aliases and I already built a muscular memory for them. However, I do agree with the article, specially when I'm pairing on another person's computer. I'm so used to my personal setup that I become ineffective in different settings. I'll try to relearn using vanilla git :)

Collapse
 
philnash profile image
Phil Nash

If aliasing core commands is wrong and git log --oneline is only 3 keystrokes more than git logo why do you have an alias for it?

Collapse
 
gonedark profile image
Jason McCreary

git log --oneline is not an alias for a core command. My issue is with an alias for git log itself, for example git l. I believe such aliases obfuscate the commands and often don't save keystrokes.

Collapse
 
philnash profile image
Phil Nash

Your comparison of git log --oneline lies in a section where you are comparing keystrokes against core commands.

And frankly, as others have said, who are you obfuscating those commands to? Only yourself. Aliases don't overwrite core commands, so you are not making it harder for anyone else to use your command line, only making it easier and more comfortable for yourself.

This so called Right Way in this blog post is simply your opinion. And you would probably do better to not force it upon others.

Collapse
 
ptraverse profile image
Philippe Traverse

You can autocomplete branches etc with single letter git ...
stackoverflow.com/questions/342969...

Collapse
 
haslo profile image
Guido Gloor Modjib

You'll like this one:

cheat = !git log -1 && git add -A && git commit --amend -C HEAD && git push -f && git log -1

Collapse
 
razbakov profile image
Aleksey Razbakov

What about g phf for git push origin HEAD --force?

Collapse
 
jorinvo profile image
jorin

most importantly

git config alias.up push 
git config alias.out pull 

git up; git out

youtube.com/watch?v=CssC-DY4lO8

Collapse
 
xdroop profile image
David Mackintosh

Ha -- I've been saying this since 2013.

Collapse
 
drrial profile image
d3rrila

git config --global alias.gud checkout --recursive dev.to/gonedark/stop-aliasing-core...

(Disclaimer: I'm aware that this isn't a legal git command)

Collapse
 
wreckitrob profile image
Rob

Unfortunately disagree with this one. I think it's down to the users preference if it's on there machine. If it's on a server it's a different story.

Collapse
 
ssjcory profile image
Cory Fisher • Edited

I use oh-my-zsh, it comes with git aliases that actually save some typing (3 chars mostly) github.com/robbyrussell/oh-my-zsh/...