Skip to content

Instantly share code, notes, and snippets.

@Kovrinic
Last active October 19, 2024 22:03
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 "[email protected]:"]
insteadOf = git://github
@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 "[email protected]:"]
  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:[email protected]"]
        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://[email protected]:1234/organization"]
  insteadOf = https://github.com/organization

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

# ~/.gitconfig
[url "[email protected]:/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