网站首页 文章专栏 将GIT的远程URL从HTTPS改为SSH
将GIT的远程URL从HTTPS改为SSH
发布 作者:被打断de狗腿 浏览量:257
这里以github为例

生成新的 SSH 密钥

  1. 打开终端

  2. 粘贴下面的文本,替换你的 GitHub 电子邮件地址。

    $ ssh-keygen -t ed25519 -C "your_email@example.com"
    

这将使用提供的电子邮件作为标签创建新的 SSH Key。

> Generating public/private algorithm key pair.
  1. 当系统提示“Enter a file in which to save the key”时,请按下 Enter 键。这接受默认文件位置。

    > Enter a file in which to save the key (path): [Press enter]
    
  2. 在提示符下,键入安全密码。

    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    
  3. 操作完成后,在默认路径会生成文件 id_ed25519.pubid_ed25519.pub

将 SSH 密钥添加到 ssh 代理

在将新的 SSH 密钥添加到 ssh 代理以管理密钥之前,应该已检查现有的 SSH 密钥并生成了新的 SSH 密钥。

  1. 在后台启动 ssh 代理。

    $ eval "$(ssh-agent -s)"
    > Agent pid 59566
    
  2. 将 SSH 私钥添加到 ssh 代理。如果使用其他名称创建了密钥,或者要添加具有不同名称的现有密钥,请将命令中的id_ed25519替换为私钥文件的名称。

    $ ssh-add ~/.ssh/id_ed25519
    
  3. 将 SSH 密钥(也就是 id_ed25519.pub 文件的内容)添加到 GitHub 上的帐户。

将远程 URL 从 HTTPS 切换到 SSH

  1. 打开终端.

  2. 将当前git目录更改为本地项目。

  3. 列出现有远程Git,以便得到你想改变的远程Git的URL。

    $ git remote -v
    > origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
    > origin  https://github.com/USERNAME/REPOSITORY.git (push)
    
  4. 用命令将你的远程URL从HTTPS改为SSH。git remote set-url

    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
    
  5. 验证远程 URL 是否已更改。

    $ git remote -v
    # Verify new remote URL
    > origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    > origin  git@github.com:USERNAME/REPOSITORY.git (push)
    
loading