Git强制使用HTTPS

问题描述 投票:1回答:1

我创建了一个GitHub仓库,然后使用SSH克隆它:

git clone [email protected]:BenReilly/all-the-characters.git

我添加了一些文件。

git add .
git commit -m "some message"
git push

这导致提示我输入用户名和密码。这本身就很奇怪,但无论如何我都进了它们。然后我得到:

> remote: Anonymous access to BenReilly/all-the-characters.git denied.
> fatal: Authentication failed for 'https://github.com/BenReilly/all-the-characters.git/'

HTTPS?什么?

git remote -v
> origin  https://github.com/BenReilly/all-the-characters.git (fetch)
> origin  https://github.com/BenReilly/all-the-characters.git (push)

啊。

git remote set-url origin [email protected]:BenReilly/all-the-characters.git
git remote -v
> origin  https://github.com/BenReilly/all-the-characters.git (fetch)
> origin  https://github.com/BenReilly/all-the-characters.git (push)

这是因为它不在我的osxkeychain中?

只是为了确保我做了ssh-add -K ~/.ssh/<key id>并确保定义了~/.ssh/config文件。行为没有变化。我还验证了密钥是在我的GitHub设置中。

我在MacOS Mojave(10.14.1)上,使用Git版本2.17.2。

为什么Git强制使用HTTPS并忽略我设置SSH地址的尝试?

.git/config文件

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = [email protected]:BenReilly/all-the-characters.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
git github ssh macos-mojave
1个回答
1
投票

Git配置有一个insteadOf option

url.<base>.insteadOf

任何以此值开头的URL都将被重写,以启动,而不是使用<base>。如果某些站点提供大量存储库,并使用多种访问方法为其提供服务,并且某些用户需要使用不同的访问方法,则此功能允许人们指定任何等效的URL并让Git自动将URL重写为特定用户的最佳替代方案,即使对于网站上前所未见的存储库也是如此。当多个insteadOf字符串与给定的URL匹配时,使用最长匹配。

基本上,如果你运行类似的东西

git config --global url.https://.insteadOf git://

你会得到一个节添加到你的全局Git配置(类Unix机器上的~/.gitconfig)看起来像

[url "https://"]
    insteadOf = git://

这会导致Git自动将git://的任何远程开头转换为https://的远程开头。使用git config --global --list查看您的全局配置,看看是否有任何insteadOf条目。

© www.soinside.com 2019 - 2024. All rights reserved.