showliuzp 2025-10-28 14:28 采纳率: 84.3%
浏览 7
已结题

golang mod tidy 访问远程仓库报错


        gitlab.sludev.shop/golang/client/im_client: module gitlab.sludev.shop/golang/client/im_client: git ls-remote -q origin in /home/x/.cache/go-mod/cache/vcs/424c2f9446a5e0ba57bef19ac5af0e43119285e8f80ee29a741abc0228a0a6f2: exit status 128:
        remote: The project you were looking for could not be found or you don't have permission to view it.
        fatal: repository 'http://oauth2:xx@gitlab.ludev.shop/golang/client.git/' not found


我尝试访问远程仓库:
1、git ls-remote gitlab.sludev.shop/golang/client/im_client

报错:
fatal: 'gitlab.sludev.shop/golang/client/im_client.git' does not appear to be a git repository
fatal: 无法读取远程仓库。

2、git ls-remote http://gitlab.sludev.shop/golang/client/im_client
      可以拉取

我的疑问:
go mod tidy拉取的时候走的什么协议,为什么2可以拉取,1却不行
  • 写回答

3条回答 默认 最新

  • 关注

    Go mod 拉取时默认会用 Git 去访问仓库,而 Git 没指定协议时,会优先试 git://ssh 协议,你这情况就是它没走 http 导致的。

    你试的第 1 条命令 git ls-remote gitlab.sludev.shop/... 没带协议,Git 可能在试 sshgit 协议,但你仓库不支持这俩,所以报错;第 2 条带了 http://,刚好是仓库支持的协议,所以能拉。

    解决很简单,给 Git 加个配置,让它访问这个域名时强制用 http 协议:
    git config --global url."http://gitlab.sludev.shop/".insteadOf "gitlab.sludev.shop/"

    这样 Go mod 再拉的时候,就会自动用 http 协议了,跟你第 2 条命令一样能成功。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月5日
  • 已采纳回答 10月28日
  • 创建了问题 10月28日