首页 > 解决方案 > 如果没有 --global 本地存储库,git config insteadOf 不起作用

问题描述

这个问题之前在git config --global insteadOf 被问过一个项目,不是全局的,但接受的解决方案对我不起作用。

# 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

所以,我的问题是:如何让 git 尊重我的本地insteadOf配置?

标签: git

解决方案


git clone当然不服从本地配置——对于克隆,还没有本地配置,只有全局配置。尝试使用本地配置的东西;git fetch/pull/push例如:

$ 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.

如果你想在url.insteadOfclone全局设置的情况下使用它,你可以在命令行中传递配置值:

$ 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.

推荐阅读