昨天我在GitHub上创建了我的第一个代码库。当进行连接时,我使用了SSH而不是HTTPS,所以我经历了一些痛苦的SSH密钥创建和连接过程。在某个时候,我卡住了,连接失败了。那一刻我想知道如何撤销我开始的这个过程,并改为使用HTTPS连接。幸运的是,今天我通过SSH成功地建立了连接,但我想知道能否更改连接类型(SSH vs HTTPS)以及如何更改。
昨天我在GitHub上创建了我的第一个代码库。当进行连接时,我使用了SSH而不是HTTPS,所以我经历了一些痛苦的SSH密钥创建和连接过程。在某个时候,我卡住了,连接失败了。那一刻我想知道如何撤销我开始的这个过程,并改为使用HTTPS连接。幸运的是,今天我通过SSH成功地建立了连接,但我想知道能否更改连接类型(SSH vs HTTPS)以及如何更改。
假设您的远程库名称为origin
,运行以下命令:
git remote set-url origin https://...
git remote set-url --push origin https://...
您可以使用git remote -v
命令查看已配置的远程库,它现在应该显示您更新后的URL。
有关更多详细信息,请参阅git-remote文档。
git remote -v
来检查您的远程设置。 - Chris...
使得理解起来有些困难。这个答案 看起来更清晰:我们将删除 git@github.com:
前缀,并将其替换为 https://github.com/
前缀,保留 <用户名>/<仓库名>
后缀(应该与 GitHub 仓库上的绿色克隆代码按钮的 URL 相同)。 - undefined以下是一些别名(单行命令),可以将您的 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://')"
它们比必要的长度稍长,以使它们具有幂等性。
/
的版本:git remote set-url origin $(git remote get-url origin | sed 's/^git@\(.*\):\/*\(.*\).git/https:\/\/\1\/\2.git/')
。 - Tim Buncegit remote set-url origin $(git remote get-url origin | sed 's/^https:\/\/\([^\/]*\)\/\(.*\).git/git@\1\:\2.git/')
(假设结果路径中不应该有前导斜杠)。 - Arjanhttps://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大家好,我曾经也遇到过这个问题,但最终找到了解决方法。
首先,请确保使用以下命令将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:
完成后,您应该能够成功使用个人访问令牌登录。
~/.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.com 和 gitlab.com 的仓库上成功测试。
注意:我使用了 -E
来进行扩展正则表达式,并且使用逗号而不是通常的斜杠来分隔替换操作的部分。这两个操作有助于减少倾斜牙签综合症。
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)
.git/config
文件,而不是使用git remote set-url
命令。你只需要注意在一些仓库服务器上它们的URL结构可能会有所不同。 - eckes