如何将GitHub的连接从SSH更改为HTTPS?

96

昨天我在GitHub上创建了我的第一个代码库。当进行连接时,我使用了SSH而不是HTTPS,所以我经历了一些痛苦的SSH密钥创建和连接过程。在某个时候,我卡住了,连接失败了。那一刻我想知道如何撤销我开始的这个过程,并改为使用HTTPS连接。幸运的是,今天我通过SSH成功地建立了连接,但我想知道能否更改连接类型(SSH vs HTTPS)以及如何更改。


如果你想将本地修改推送到 github,最好保持 ssh 连接。阅读一些 ssh 教程,并配置私钥和公钥,以避免多次输入密码。 - Basile Starynkevitch
1
@BasileStarynkevitch,SSH和HTTPS连接都可以用于向GitHub(以及许多其他主机)“push”。 - Chris
我通常会编辑.git/config文件,而不是使用git remote set-url命令。你只需要注意在一些仓库服务器上它们的URL结构可能会有所不同。 - eckes
我经常使用https作为fetch url和ssh作为Push url,这样的好处是我不需要为随机的fetches解锁我的ssh密钥。 - eckes
阅读答案后,如果您想更改子模块的连接:如何更改git子模块的远程存储库? - li ki
5个回答

156

假设您的远程库名称为origin,运行以下命令:

  • git remote set-url origin https://...
  • git remote set-url --push origin https://...

您可以使用git remote -v命令查看已配置的远程库,它现在应该显示您更新后的URL。

有关更多详细信息,请参阅git-remote文档


它仍然每次都要求输入SSH密钥的密码吗? - dragonfly02
1
@dragonfly02,不是的。如果它要求输入SSH密码短语,那么它仍在使用SSH。请按照答案中所述运行git remote -v来检查您的远程设置。 - Chris
省略的 ... 使得理解起来有些困难。这个答案 看起来更清晰:我们将删除 git@github.com: 前缀,并将其替换为 https://github.com/ 前缀,保留 <用户名>/<仓库名> 后缀(应该与 GitHub 仓库上的绿色克隆代码按钮的 URL 相同)。 - undefined

17

以下是一些别名(单行命令),可以将您的 repo 从 ssh 切换到 https,反之亦然。假设您的默认远程名称为 origin,且您的远程是 github.com。

alias git-https="git remote set-url origin https://github.com/$(git remote get-url origin | sed 's/https:\/\/github.com\///' | sed 's/git@github.com://')"
alias git-ssh="  git remote set-url origin git@github.com:$(    git remote get-url origin | sed 's/https:\/\/github.com\///' | sed 's/git@github.com://')"

它们比必要的长度稍长,以使它们具有幂等性。


10
谢谢您。这是一个适用于非github.com和URL路径包含或不包含前导/的版本:git remote set-url origin $(git remote get-url origin | sed 's/^git@\(.*\):\/*\(.*\).git/https:\/\/\1\/\2.git/') - Tim Bunce
1
..并且是@TimBunce版本的反向操作:git remote set-url origin $(git remote get-url origin | sed 's/^https:\/\/\([^\/]*\)\/\(.*\).git/git@\1\:\2.git/')(假设结果路径中不应该有前导斜杠)。 - Arjan
对于 AWS 代码提交的 URL(例如 https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/somerepo),您可以轻松地切换 https 和 ssh:git remote set-url origin $(git remote get-url origin | sed 's/^ssh:/https:/') 或者切换到 SSH git remote set-url origin $(git remote get-url origin | sed 's/^https:/ssh:/') - phhu

5

大家好,我曾经也遇到过这个问题,但最终找到了解决方法。

首先,请确保使用以下命令将git更新至最新版本:

C:\> git update-git-for-windows

然后运行以下命令:

C:\>git config --global url."https://github.com/".insteadOf git@github.com:

接着运行以下命令:

C:\>git config --global url."https://".insteadOf git://

如果您仍然遇到“Permission denied (publickey)”错误,您可以手动进行此过程,具体如下:

导航到您的.gitconfig文件。

您可以使用以下命令查找其位置:

git config --list --show-origin

使用记事本打开该文件。

删除此部分内容:

[url "git://"]
    insteadOf = https://
[url "insteadOf = git@github.com:"]
    insteadOf = https://github.com/

替换为:

[url "https://"]
    insteadOf = git://
[url "https://github.com/"]
    insteadOf = git@github.com:

完成后,您应该能够成功使用个人访问令牌登录。


3
请在您的~/.bashrc文件中添加以下别名定义:
alias git-ssh='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^https://([^/]*)/(.*)$,git@\1:\2,'\'')"'

alias git-https='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^git@([^:]*):/*(.*)$,https://\1/\2,'\'')"'

接下来,

  • https 切换到 ssh:使用 git-ssh
  • ssh 切换到 https:使用 git-https

已在 github.comgitlab.com 的仓库上成功测试。

注意:我使用了 -E 来进行扩展正则表达式,并且使用逗号而不是通常的斜杠来分隔替换操作的部分。这两个操作有助于减少倾斜牙签综合症


1
  git remote -v
# View existing remotes
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

    git remote set-url origin https://github.com/user/repo2.git

# Change the 'origin' remote's URL

    git remote -v

# Verify new remote URL
# origin  https://github.com/user/repo2.git (fetch)
# origin  https://github.com/user/repo2.git (push)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接