Git 强制使用 HTTPS

Git forcing use of HTTPS

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

git clone git@github.com: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 git@github.com: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 = git@github.com:BenReilly/all-the-characters.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

Git 配置有一个 insteadOf option:

url.<base>.insteadOf

Any URL that starts with this value will be rewritten to start, instead, with <base>. In cases where some site serves a large number of repositories, and serves them with multiple access methods, and some users need to use different access methods, this feature allows people to specify any of the equivalent URLs and have Git automatically rewrite the URL to the best alternative for the particular user, even for a never-before-seen repository on the site. When more than one insteadOf strings match a given URL, the longest match is used.

基本上,如果你运行像

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

您会在全局 Git 配置中添加一个节(~/.gitconfig 在类 Unix 机器上),看起来像

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

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