Archive for August, 2014

Useful Git Commands: URL Rewriting

29 Aug 2014

People have to use SSH or HTTPS to push to GitHub, but when fetching one can use git’s own network protocol because it is generally faster. You can make a specific repository on your machine use SSH only for pushing by cloning it with the faster git:// URL and running something like this:

% git config remote.upstream.pushurl git@github.com:gholms/boto

That works nicely, but you have to do it once for every single repository you want to interact with. That quickly becomes annoying. Thankfully, you can leverage git’s URL-rewriting mechanism to make this easier:

% git config --global url.git://github.com/.insteadOf github:
% git config --global url.git@github.com:.pushInsteadOf github:

This adds two new rules to your git configuration:

  1. If a URL starts with github: then replace that with git://github.com/.
  2. If a URL starts with github: and you are pushing then replace that with git@github.com:.

After you do that you can simply use URLs like github:gholms/boto when cloning. They will get rewritten to git@github.com:gholms/boto when pushing, and git://github.com/gholms/boto the rest of the time, speeding things up without creating additional work in the future.

This should work if you prefer HTTPS for pushing to GitHub, or if you use other servers, too. Just tweak the commands.