当前位置: 首页> 房产> 建筑 > git 提交代码由原先账号密码调整为ssh

git 提交代码由原先账号密码调整为ssh

时间:2025/8/23 9:24:14来源:https://blog.csdn.net/qq_39698985/article/details/141792008 浏览次数:0次

如果你希望将 Git 提交代码的身份验证方式从用户名和密码切换到 SSH,你需要进行以下几个步骤:

1. 生成 SSH 密钥对

如果你还没有 SSH 密钥对,可以使用以下命令生成一个新的密钥对:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • -t rsa 指定密钥类型为 RSA。
  • -b 4096 指定密钥长度为 4096 位。
  • -C "your_email@example.com" 添加一个注释(通常是你的邮箱)。

按照提示选择保存位置(默认是 ~/.ssh/id_rsa)和设置密码(如果需要)。

2. 将公钥添加到你的 Git 账户

找到生成的公钥(~/.ssh/id_rsa.pub)并将其添加到你的 Git 账户(如 GitHub、GitLab 或 Bitbucket):

  1. 查看公钥内容:

    cat ~/.ssh/id_rsa.pub
    
  2. 将公钥复制到剪贴板:

    cat ~/.ssh/id_rsa.pub | clip  # Windows
    pbcopy < ~/.ssh/id_rsa.pub     # macOS
    xclip -sel clip < ~/.ssh/id_rsa.pub  # Linux
    
  3. 登录到你的 Git 账户,进入 SSH 密钥设置页面,粘贴公钥并保存。

    • GitHub: GitHub SSH Key Settings
    • GitLab: GitLab SSH Key Settings
    • Bitbucket: Bitbucket SSH Key Settings

3. 配置 Git 使用 SSH

更新 Git 远程仓库的 URL,使用 SSH 协议代替 HTTPS:

  1. 查看当前的远程仓库 URL:

    git remote -v
    
  2. 将远程仓库 URL 更改为 SSH:

    • GitHub:

      git remote set-url origin git@github.com:username/repository.git
      
    • GitLab:

      git remote set-url origin git@gitlab.com:username/repository.git
      
    • Bitbucket:

      git remote set-url origin git@bitbucket.org:username/repository.git
      

    usernamerepository 替换为你的用户名和仓库名。

  3. 验证配置是否正确:

    git remote -v
    

    确保显示的 URL 使用 SSH 协议(git@...)。

4. 测试 SSH 连接

使用以下命令测试与远程仓库的 SSH 连接:

  • GitHub:

    ssh -T git@github.com
    
  • GitLab:

    ssh -T git@gitlab.com
    
  • Bitbucket:

    ssh -T git@bitbucket.org
    

如果一切设置正确,你应该看到一条欢迎消息或成功连接的提示。

完成上述步骤后,你的 Git 提交代码将使用 SSH 密钥进行身份验证,而不再需要输入用户名和密码。

关键字:git 提交代码由原先账号密码调整为ssh

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: