Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git config insteadOf doesn't work without --global for local repository

Tags:

git

This question has been asked before at git config --global insteadOf for a project, not global but the accepted solution doesn't work for me.

# Random directory

$ pwd
$ /tmp/nwani_1585850170


# Now, a local repo

$ git init
Initialized empty Git repository in /tmp/nwani_1585850170/.git/


# Pretty modern git (as of April, 2020)

$ git --version
git version 2.21.1


# Don't have any existing config

$ git config --list | grep url


# Let's configure the insteadOf thingy for the local repo:

$ git config url."ssh://".insteadOf "https://"


# Verify that it indeed got added:

$ git config --list | grep url
url.ssh://.insteadof=https://


# Yes, it is in the local config

$ cat .git/config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[url "ssh://"]
    insteadOf = https://


# Check if it works? Spoiler alert: It doesn't!

$ GIT_CURL_VERBOSE=1 GIT_TRACE=1 git clone --progress --verbose https://domain.doesnt.exist/blah
13:57:17.426161 git.c:419               trace: built-in: git clone --progress --verbose https://domain.doesnt.exist/blah
Cloning into 'blah'...
13:57:17.427962 run-command.c:643       trace: run_command: git-remote-https origin https://domain.doesnt.exist/blah
* Couldn't find host domain.doesnt.exist in the .netrc file; using defaults
*   Trying 92.242.140.21:443...
* TCP_NODELAY set
^C


# As one can see in the trace above, it runs the command git-remote-https, so it didn't honor the insteadOf thingy.


# Now let's make the config global:

$ git config --global url."ssh://".insteadOf "https://"


# Let's try it now. Spoiler alert: It works!

GIT_CURL_VERBOSE=1 GIT_TRACE=1 git clone --progress --verbose https://domain.doesnt.exist/blah
13:57:48.910586 git.c:419               trace: built-in: git clone --progress --verbose https://domain.doesnt.exist/blah
Cloning into 'blah'...
13:57:48.912966 run-command.c:643       trace: run_command: unset GIT_DIR; ssh domain.doesnt.exist 'git-upload-pack '\''/blah'\'''
^C

# As one can see, now it is using the ssh command

So, my query is: How do I make git honor my local insteadOf config?

like image 664
Nehal J Wani Avatar asked Oct 28 '25 05:10

Nehal J Wani


1 Answers

git clone certainly doesn't obey local config — for cloning there is no local config yet, only global. Try something that uses local config; git fetch/pull/push for example:

$ GIT_CURL_VERBOSE=1 GIT_TRACE=1 git fetch https://domain.doesnt.exist/blah master
22:21:01.075552 git.c:371               trace: built-in: git 'fetch' 'https://domain.doesnt.exist/blah' 'master'
22:21:01.076706 run-command.c:350       trace: run_command: 'ssh' 'domain.doesnt.exist' 'git-upload-pack '\''/blah'\'''
ssh: Could not resolve hostname domain.doesnt.exist: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

If you want to use url.insteadOf for clone without setting it globally you can pass config value in the command line:

$ GIT_CURL_VERBOSE=1 GIT_TRACE=1 git -c url.ssh://.insteadof=https:// clone --verbose https://domain.doesnt.exist/blah
22:25:33.588762 git.c:371               trace: built-in: git 'clone' '--verbose' 'https://domain.doesnt.exist/blah'
Cloning into 'blah'...
22:25:33.596631 run-command.c:350       trace: run_command: 'ssh' 'domain.doesnt.exist' 'git-upload-pack '\''/blah'\'''
ssh: Could not resolve hostname domain.doesnt.exist: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
like image 87
phd Avatar answered Oct 29 '25 20:10

phd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!