Skip to content

Instantly share code, notes, and snippets.

@Kovrinic
Last active November 28, 2024 04:25
Show Gist options
  • Save Kovrinic/ea5e7123ab5c97d451804ea222ecd78a to your computer and use it in GitHub Desktop.
Save Kovrinic/ea5e7123ab5c97d451804ea222ecd78a to your computer and use it in GitHub Desktop.
git global url insteadOf setup
# one or the other, NOT both
[url "https://github"]
insteadOf = git://github
# or
[url "git@github.com:"]
insteadOf = git://github

Replace git:// with https://

Rewrite any git:// urls to be https:// but, it won't touch sshurls (git@github.com:)

git config --global url."https://github".insteadOf git://github

or replace with ssh

Use ssh instead of https://

git config --global url."git@github.com:".insteadOf "https://github.com/"

sauce: https://gist.github.com/grawity/4392747 & @hansdg1

@fantasyczl
Copy link

Thanks

@Kastilo
Copy link

Kastilo commented Sep 30, 2021

Hello, I tried to do this, so I attempted to git clone with https to see if it would override to ssh, but it seems to keep failing, is there anyway to force this?

Picture Link of Issue

@timmyyuan
Copy link

Pardon me, how to unset insteadOf in git global settings?

@tribela
Copy link

tribela commented Jan 17, 2022

@timmyyuan Pardon me, how to unset insteadOf in git global settings?

git config --global -e bring the editor for your git config. remove related setting on there

@timmyyuan
Copy link

Thanks @tribela , I have found another way to remove this setting:

git config --global --remove-section url."https://github.com/"

@IzhakJakov
Copy link

Is there a way to do something like that instead?

[url "https://github.com"]
  insteadOf = github.com/

# or

[url "git@github.com:"]
  insteadOf = github.com/

Basically without using the http:// or https:// prefix.

@tommy70404
Copy link

Thanks!

@frenchbeast
Copy link

Exactly what i was looking for thanks 👍

@tecnocat
Copy link

tecnocat commented Jan 9, 2023

It is possible to remove entries with credentials from command line scripts?

[url "https://user:token@repository.url.com"]
        insteadOf = https://repository.url.com

I tried git config --global --unset name <pattern> with no luck :(

@toddwalstad-eaton
Copy link

I am trying to do the same to remove old entries also with no luck. This pattern or [value_regex] as described in the document doesn't seem to work these url overrides.

@vuquang23
Copy link

thank !! 👍

@pklapperich
Copy link

Do you know how to set up with custom port? the default is 22

you have two options. In the below examples, the match is specific to an organization (ex you want to force ssh when working with your company's projects but https is fine for public projects). Just filter on and replace less if you want less.

Option 1: Entirely in gitconfig. The ssh:// is required to change ports.

# ~/.gitconfig
[url "ssh://git@github.com:1234/organization"]
  insteadOf = https://github.com/organization

Option 2: Use replacement in git config as well as replacement in ssh config.

# ~/.gitconfig
[url "git@github.com:/organization"]
  insteadOf = https://github.com/organization
```

```
# ~/.ssh/config
Host github.com github
    Hostname github.com
    User git
    Port 1234
```

Basically anything that you can set with `-o [optionName]` on the ssh commandline can be set via the config file. The line `Host` is space separated list of matching names that you type on the commandline (ex: `ssh github`) or use in URLs for things that use ssh under the hood (rsync, git, svn, etc). The `Hostname ...` option is the hostname that's used in-place-of what was matched.

@jasong-au
Copy link

Any way to debug this? I'm trying to set something up for 'go install' to use a private repository and personal access token, but I can never tell if it is being called or not.

@pklapperich
Copy link

@jasong-au Any time git is called, it'll check the ~/.gitconfig file for matches based on insteadOf. This includes if go install is using git under the hood. Any time ssh is called, it'll check ~/.ssh/config for matching host definitions. This includes if git is using ssh under the hood.

So you can debug a ~/.ssh/config file using ssh, such as ssh github or ssh github.com and ensure it uses the hostname, username, and port defined in the config file.

You can debug a ~/.gitconfig using git.

Do something like this:

$ cat ~/.gitconfig
[url "https://gist.github.com/ea5e7123ab5c97d451804ea222ecd78a.git"]
   insteadOf = http://jasong-au

$ git clone http://jasong-au
Cloning into 'jasong-au'...
remote: Enumerating objects: 15, done.
remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 15 (from 1)
Receiving objects: 100% (15/15), done.
Resolving deltas: 100% (3/3), done.
(base)

Just make sure that whatever you put in your insteadOf = clause properly matches a URL for a git repo that go install is trying to access. And keep in mind that it's a find/replace matching from the start of the string, so you can swap any part of the start of the URL you want, including the entire URL. In my config I swap https://ourserver.organziation.example.com matching based on insteadOf = https://github.com/organization/ because we moved away from github and now have our own internal git server. By setting up the url mapping I didn't have to go through and update the URL for every repo already cloned on my workstation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment