Commit 6d0b8fab authored by Jamie Tanna's avatar Jamie Tanna

Add article about Git's `url.insteadOf`

Closes #391.
parent 1e41178f
Pipeline #52726117 passed with stages
in 7 minutes and 5 seconds
---
title: "Easily rewriting Git URLs from HTTPS to SSH and vice versa"
description: "How to use Git's config to rewrite HTTPS URLs to SSH and vice versa, for repo pushes and pulls."
categories:
- blogumentation
- git
tags:
- blogumentation
- git
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2019-03-20
slug: "git-rewrite-url-https-ssh"
image: /img/vendor/git.png
---
If you're working with Git repos regularly, be it for personal projects, Open Source work, or your day job, you may have have discovered [SSH keys] and using them to push/pull over SSH connections. They're a great way to remove the reliance on entering your username/password combination, as well as having a number of other security benefits.
One key issue with using SSH keys for authenticating yourself with your Git host is that you need to have thought about it before you clone a repo, or at least before you push your changes. That's because at the time it's making a connection to the remote, Git needs to know what protocol to use. And if you've previously cloned it over HTTPS, Git will respect that and not know that you want to upgrade to SSH.
So what you may find yourself doing is manually rewriting the URL yourself each-and-every time, i.e. by running :
```sh
git remote set-url origin git@gitlab.com/...
```
That is, until you know that Git comes with the ability to configure this, by adding the following to your [Git config] either globally (`~/.gitconfig`) or per-repo (`.git/config`):
```ini
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
[url "ssh://git@gitlab.com/"]
insteadOf = https://gitlab.com/
```
Alternatively, you can run the following commands (adding in `--global` as necessary):
```sh
git config url.ssh://git@github.com/.insteadOf https://github.com/
git config url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/
```
This then instructs Git to rewrite your URLs as necessary when pulling/pushing from your repos - awesome!
However, this may not be exactly what you want, as you may only want to push over SSH, but pull over HTTPS. Fortunately Git also provides the ability to use `pushInsteadOf` for this, configured as such:
```ini
[url "ssh://git@github.com/"]
pushInsteadOf = https://github.com/
[url "ssh://git@gitlab.com/"]
pushInsteadOf = https://gitlab.com/
```
Begone the woes of manually rewriting your Git URLs!
Note that this can be used with all manner of Git hosts, including your organisation's internal Git repository.
[SSH keys]: https://docs.gitlab.com/ee/ssh/README.html
[Git config]: https://git-scm.com/docs/git-config
posts:
- https://www.jvt.me/posts/2019/03/20/git-rewrite-url-https-ssh/
- https://www.jvt.me/posts/2019/03/19/microformats-hentry/
- https://www.jvt.me/posts/2019/03/18/displaying-webmentions/
- https://www.jvt.me/posts/2019/03/15/personal-hcard/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment