Init_Git
0 forks
新设备初始化SSH协议与Git设置
id: 46
96 Lines
  • Markdown 100%
README.md

从生成 SSH 密钥到配置 Git 个人信息的完整流程,分为以下四个步骤:


第一步:设置 Git 个人配置(用户名与邮箱)

配置 Git 提交代码时显示的作者信息。

  1. 设置全局用户名和邮箱:

    git config --global user.name "你的名字或昵称"
    git config --global user.email "your_email@example.com"
    
  2. 常用建议配置(可选):

    • 换行符处理(防止跨系统换行符混乱):
      • Windows 用户:
        git config --global core.autocrlf true
        
      • macOS / Linux 用户:
        git config --global core.autocrlf input
        

第二步:验证配置

你可以运行以下命令检查配置是否生效:

# 查看所有全局配置
git config --global --list

# 或者单独查看用户名和邮箱
git config user.name
git config user.email

补充提示: 如果你在某个特定仓库需要使用不同的身份(例如工作仓库使用公司邮箱),可以在该仓库目录下去掉 --global 参数重新设置:

git config user.name "Work Name"
git config user.email "work@company.com"

第三步:生成新的 SSH 密钥

目前推荐使用更安全、更高效的 Ed25519 算法(也可以使用 RSA 4096)。

  1. 打开终端(Linux/macOS 的 Terminal,或者 Windows 的 Git Bash)。

  2. 执行以下命令(邮箱如实填写,后续注册账户需要保持一致):

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

    (如果需要使用 RSA 算法,命令为:ssh-keygen -t rsa -b 4096 -C "your_email@example.com")

  3. 命令行会提示以下问题,按需处理:

    • 保存路径:Enter file in which to save the key (/Users/you/.ssh/id_ed25519):
      • 如果这是你唯一的密钥,直接按回车使用默认路径。
    • 密码短语(Passphrase):Enter passphrase (empty for no passphrase):
      • 建议直接按回车跳过(留空);如果为了安全设置了密码,以后每次推送代码都需要输入该密码。

第四步:将公钥发送给管理员

生成后,你的 ~/.ssh 目录下会有两个文件:

  • id_ed25519:私钥(绝对不要泄露给任何人)
  • id_ed25519.pub:公钥(发送给管理员)
  1. 查看并复制公钥内容:

    • Linux / MacOS:
      cat ~/.ssh/id_ed25519.pub
      

      (MacOS 也可直接运行 pbcopy < ~/.ssh/id_ed25519.pub 自动复制到剪贴板)

    • Windows (Git Bash):
      cat ~/.ssh/id_ed25519.pub
      

      (或者运行 clip < ~/.ssh/id_ed25519.pub 自动复制)

  2. 添加到平台:

    • 将公钥id_ed25519.pub发送给管理员。
  3. 测试连接: 在终端运行对应平台的测试命令:

    # 测试 Git.FieryMaple.cn
    ssh -T git@git.fierymaple.cn
    

    首次连接会提示 Are you sure you want to continue connecting (yes/no/[fingerprint])?,输入 yes 即可。看到包含 successfully authenticated 说明连接成功。

Please wait...
Connection lost or session expired, reload to recover
Page is in error, reload to recover