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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30683399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:05:40  来源:igfitidea点击:

How to change a connection to GitHub from SSH to HTTPS?

gitgithubsshhttpsgit-remote

提问by dickbarba

I created my first repository in GitHub yesterday. When making the connection I used SSH instead of HTTPS, so I went through a little painful SSH key creation and connection process. At some point I got stuck and the connection failed. I wondered at that moment how I could revert the process I started and begin with a HTTPS connection instead. Happily, today I got the connection working through SSH but I'm wondering about the value of being able to change the type of connection (SSH vs HTTPS) and if there is a way to do it.

我昨天在 GitHub 上创建了我的第一个存储库。在建立连接时,我使用 SSH 而不是 HTTPS,所以我经历了一些痛苦的 SSH 密钥创建和连接过程。在某些时候,我卡住了,连接失败。那一刻我想知道如何恢复我开始的过程并从 HTTPS 连接开始。令人高兴的是,今天我通过 SSH 建立了连接,但我想知道能够更改连接类型(SSH 与 HTTPS)的价值以及是否有办法做到这一点。

回答by Chris

Assuming your remote is called origin, run

假设你的遥控器被调用origin,运行

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

You can view the configured remotes with git remote -v, which should now show your updated URLs.

您可以使用 来查看配置的遥控器git remote -v,它现在应该会显示您更新后的 URL。

See the documentation for git-remotefor more details.

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

回答by Harry Moreno

here are some aliases (oneliners) to switch your repo from ssh to https and back. Assuming your default remote is named originand your remote is github.com

这里有一些别名(oneliners)可以将您的存储库从 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/[email protected]://')"
alias git-ssh="  git remote set-url origin [email protected]:$(    git remote get-url origin | sed 's/https:\/\/github.com\///' | sed 's/[email protected]://')"

they're a bit longer than necessary to make them idempotent

它们比使它们幂等所需的时间要长一些