我的计算机中有两个不同的工作文件夹,一个位于 ~/Documents/work/companyA 中,另一个位于 ~/work/companyB 中。我写了条件 gitconfig,就像
[includeIf "gitdir:~/Documents/work/companyA/"]
path = ~/Documents/work/companyA/.gitconfig
[includeIf "gitdir:~/Documents/work/companyB/"]
path = ~/Documents/work/companyB/.gitconfig
这两个文件的内容就像
[user]
name = companyAuser
email = [email protected]
[url "ssh://git@github-companyA/"]
insteadOf = https://github.com/
我知道这个文件正在被读取并且 git@github-companyA 是有效的,因为当我运行时
git config user.email
ssh -T git@github-companyA
在 ~/Documents/work/companyA/ 内的目录中,这两个命令都可以正常工作。
我正在使用此配置从私有存储库“获取”。另外,当我将 URL 重写命令放入 ~/.gitconfig 时,它工作正常。我该如何解决这个问题
我误读了你的问题:这不是一个 git 问题,它实际上是一个“go get + git”问题。
go get
使用的位置位于GOMODCACHE
目录下,而不是在您的工作目录下。
这是在
strace
命令上使用 go get github.com/spf13/cast
的插图(我通过设置 GOPRIVATE=github.com/spf13/cast
模拟了私人存储库):
...
[pid 711376] chdir("/home/LeGEC/pkg/mod/cache/vcs/60c669232b830322cfc3d9e570d86c2297d36fdbc38de8ad322b59a3dff47a24") = 0
[pid 711376] execve("/usr/bin/git", ["git", "init", "--bare"], 0xc00046a000 /* 94 vars */) = 0
...
[pid 711377] execve("/usr/bin/git", ["git", "remote", "add", "origin", "--", "https://github.com/spf13/cast"], 0xc00046a300 /* 94 vars */) = 0
...
可能的解决方法:
github.com/companyA/
而不是全部github.com/
),您可以全局设置替换网址,例如:# in your ~/.gitconfig:
[url "ssh://git@github-companyA/companyA/"]
insteadOf = https://github.com/companyA/
go get
命令之一的持续时间内,使用 环境变量之一指定 gitconfig 文件;例如:
GIT_CONFIG_GLOBAL=~/Documents/work/companyA/.gitconfig go get ...
GOMODCACHE
的存储库时,将
~/Documents/work/companyA/
设置为
companyA