如何将到 GitHub 的连接从 SSH 更改为 HTTPS?
How to change a connection to GitHub from SSH to HTTPS?
我昨天在 GitHub 创建了我的第一个存储库。在建立连接时,我使用的是 SSH 而不是 HTTPS,因此我经历了一个有点痛苦的 SSH 密钥创建和连接过程。有时我卡住了,连接失败了。那一刻我想知道如何恢复我开始的过程并开始使用 HTTPS 连接。令人高兴的是,今天我通过 SSH 建立了连接,但我想知道能够更改连接类型(SSH 与 HTTPS)的价值以及是否有办法做到这一点。
假设您的遥控器名为 origin
、运行
git remote set-url origin https://...
git remote set-url --push origin https://...
您可以使用 git remote -v
查看配置的遥控器,现在应该会显示更新后的网址。
有关详细信息,请参阅 the documentation for git-remote
。
这里有一些别名 (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/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://')"
它们比幂等性所需的要长一些
将这些别名定义放入您的 ~/.bashrc
:
alias git-ssh='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^https://([^/]*)/(.*)$,git@:,'\'')"'
alias git-https='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^git@([^:]*):/*(.*)$,https:///,'\'')"'
然后,
- 从
https
切换到ssh
:git-ssh
- 从
ssh
切换到https
:git-https
已成功测试 github.com 和 gitlab.com 回购协议。
注意:我使用 -E
作为扩展正则表达式,我使用逗号而不是通常的斜杠来分隔替换操作的各个部分。这些共同帮助减少了 leaning toothpick syndrome.
伙计们,我为这个问题苦苦挣扎了一段时间,但终于找到了解决方法。
首先确保您已使用以下方法将 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://
如果您仍然收到权限被拒绝(公钥)错误,您可以手动执行以下过程:
导航到您的 .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:
在此之后,您应该能够使用您的个人访问令牌成功登录。
我昨天在 GitHub 创建了我的第一个存储库。在建立连接时,我使用的是 SSH 而不是 HTTPS,因此我经历了一个有点痛苦的 SSH 密钥创建和连接过程。有时我卡住了,连接失败了。那一刻我想知道如何恢复我开始的过程并开始使用 HTTPS 连接。令人高兴的是,今天我通过 SSH 建立了连接,但我想知道能够更改连接类型(SSH 与 HTTPS)的价值以及是否有办法做到这一点。
假设您的遥控器名为 origin
、运行
git remote set-url origin https://...
git remote set-url --push origin https://...
您可以使用 git remote -v
查看配置的遥控器,现在应该会显示更新后的网址。
有关详细信息,请参阅 the documentation for git-remote
。
这里有一些别名 (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/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://')"
它们比幂等性所需的要长一些
将这些别名定义放入您的 ~/.bashrc
:
alias git-ssh='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^https://([^/]*)/(.*)$,git@:,'\'')"'
alias git-https='git remote set-url origin "$(git remote get-url origin | sed -E '\''s,^git@([^:]*):/*(.*)$,https:///,'\'')"'
然后,
- 从
https
切换到ssh
:git-ssh
- 从
ssh
切换到https
:git-https
已成功测试 github.com 和 gitlab.com 回购协议。
注意:我使用 -E
作为扩展正则表达式,我使用逗号而不是通常的斜杠来分隔替换操作的各个部分。这些共同帮助减少了 leaning toothpick syndrome.
伙计们,我为这个问题苦苦挣扎了一段时间,但终于找到了解决方法。 首先确保您已使用以下方法将 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://
如果您仍然收到权限被拒绝(公钥)错误,您可以手动执行以下过程:
导航到您的 .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:
在此之后,您应该能够使用您的个人访问令牌成功登录。