Git 强制使用 HTTPS

The*_*e E 3 git ssh github macos-mojave

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

git clone git@github.com:BenReilly/all-the-characters.git
Run Code Online (Sandbox Code Playgroud)

我添加了一些文件。

git add .
git commit -m "some message"
git push
Run Code Online (Sandbox Code Playgroud)

这导致系统提示我输入用户名和密码。这本身就有点奇怪,但我还是输入了它们。然后我得到:

> remote: Anonymous access to BenReilly/all-the-characters.git denied.
> fatal: Authentication failed for 'https://github.com/BenReilly/all-the-characters.git/'
Run Code Online (Sandbox Code Playgroud)

HTTPS?什么?

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

啊。

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)
Run Code Online (Sandbox Code Playgroud)

这是因为它不在我的 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
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 8

Git 配置有一个insteadOf选项

url.<base>.insteadOf

任何以此值开头的 URL 将被重写为以<base>. 如果某些站点提供大量存储库,并为它们提供多种访问方法,并且某些用户需要使用不同的访问方法,则此功能允许人们指定任何等效的 URL,并让 Git 自动将 URL 重写为对于特定用户来说,这是最好的选择,即使对于网站上从未见过的存储库也是如此。当多个 otherOf 字符串与给定 URL 匹配时,将使用最长的匹配项。

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

git config --global url.https://.insteadOf git://
Run Code Online (Sandbox Code Playgroud)

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

[url "https://"]
    insteadOf = git://
Run Code Online (Sandbox Code Playgroud)

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