DEV Community

Rob Hoelz
Rob Hoelz

Posted on

Adding Remote Shortcuts to Git

Originally posted at https://hoelz.ro/blog/adding-remote-shortcuts-to-git

If you're like me, chances are you're a Git user that uses a small set of hosts for repositories very frequently. The example I'll use here is GitHub.

To clone another user's repository, you end up typing out something like this:

  $ git clone https://github.com/miyagawa/cpanminus.git
Enter fullscreen mode Exit fullscreen mode

If you want to clone one of your one repositories, you end up with something like this:

  $ git clone git@github.com:hoelzro/linotify.git
Enter fullscreen mode Exit fullscreen mode

Now, that isn't that much typing, but there's got to be a shorter way! Wouldn't it be nice if I could just type this?

  $ git clone github:miyagawa/cpanminus
Enter fullscreen mode Exit fullscreen mode

Or this?

  $ git clone hoelzro:linotify
Enter fullscreen mode Exit fullscreen mode

Well, it turns out that with a few changes to your .gitconfig, you can!

You can add a URL section to your gitconfig, with an insteadOf attribute that describes the prefix you'd like to use. Here's how the previous two examples look in my .gitconfig:

[url "git@github.com:hoelzro/"]
    insteadOf = hoelzro:
[url "https://github.com/"]
    insteadOf = github:
Enter fullscreen mode Exit fullscreen mode

Rinse and repeat for your various sources!

Top comments (5)

Collapse
 
shreyasminocha profile image
Shreyas Minocha

Nice, I didn't know you could do that natively. I've been using hub aliased as git and it does this as well as some other cool stuff.

Collapse
 
hoelzro profile image
Rob Hoelz

Neat! What other kinds of cool stuff does hub do?

Collapse
 
shreyasminocha profile image
Shreyas Minocha

It provides a pull-request and fork, commands that do what you'd expect them to. It gives you ci-status which returns the CI status for a given commit. release and issue commands can be used to access those Github features from within the command line. create sets up your repo on Github, browse opens the Github page for the current repo and there's compare which I haven't used much, really.

Overall, it's awesome.

Collapse
 
evanoman profile image
Evan Oman • Edited

Nice to know you can do this from git! I like manage these names with my ~/.ssh/config file (which allows me to set the identity file for each remote as well)

Collapse
 
hoelzro profile image
Rob Hoelz

Yeah, this technique pairs very well with ~/.ssh/config!