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

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

1. 生成 SSH 密钥对

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

bash 复制代码
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. 查看公钥内容:

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

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

3. 配置 Git 使用 SSH

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

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

    bash 复制代码
    git remote -v
  2. 将远程仓库 URL 更改为 SSH:

    • GitHub:

      bash 复制代码
      git remote set-url origin git@github.com:username/repository.git
    • GitLab:

      bash 复制代码
      git remote set-url origin git@gitlab.com:username/repository.git
    • Bitbucket:

      bash 复制代码
      git remote set-url origin git@bitbucket.org:username/repository.git

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

  3. 验证配置是否正确:

    bash 复制代码
    git remote -v

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

4. 测试 SSH 连接

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

  • GitHub:

    bash 复制代码
    ssh -T git@github.com
  • GitLab:

    bash 复制代码
    ssh -T git@gitlab.com
  • Bitbucket:

    bash 复制代码
    ssh -T git@bitbucket.org

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

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

相关推荐
JuiceFS16 小时前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
chen9451 天前
mysql 3节点mgr集群部署
运维·后端
LH_R1 天前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
dessler1 天前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
权咚2 天前
阿权的开发经验小集
git·ios·xcode
少妇的美梦2 天前
logstash教程
运维
chen9452 天前
k8s集群部署vector日志采集器
运维
chen9452 天前
aws ec2部署harbor,使用s3存储
运维
東雪蓮☆2 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_264220892 天前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs