01 - GitHub 账号配置与 SSH 密钥
本章目标:注册 GitHub 账号,配置好 SSH 密钥,做到"克隆仓库不用输密码"。
一、注册 GitHub 账号
操作位置:🌐 浏览器
1. 打开 https://github.com
2. 点击右上角 Sign up
3. 填写:
- Email(用你的个人邮箱,别用公司邮箱)
- Password
- Username(想好再改,虽然可以改但有冷却期)
4. 完成人机验证
5. 去邮箱收验证码,完成注册
个人资料设置
🌐 浏览器操作:
1. 点击右上角头像 → Settings → Profile
2. 设置:
- Profile photo(放个头像,别用默认的)
- Bio(写一句话介绍自己)
- Location(写 China 或具体城市)
开启两步验证(2FA)
🌐 浏览器操作:
1. Settings → Password and authentication → Two-factor authentication
2. 用手机 App(推荐 Authy 或 Google Authenticator)扫码绑定
3. 保存好恢复代码(Recovery codes),存到安全的地方
二、生成 SSH 密钥
操作位置:💻 Git Bash
打开 Git Bash(不是浏览器,不是 CMD),输入下面的命令。
bash
# 第一步:检查是否已有密钥
ls ~/.ssh/
# 如果看到 id_ed25519.pub 或 id_rsa.pub,说明之前生成过
# 可以跳过生成步骤,直接去"三、把公钥添加到 GitHub"
# 第二步:生成新密钥
# 用你自己注册 GitHub 的邮箱
ssh-keygen -t ed25519 -C "你的邮箱@example.com"
# 然后会问你三个问题,全部直接按回车:
# Enter file in which to save the key: ← 直接回车
# Enter passphrase: ← 直接回车(不设密码)
# Enter same passphrase again: ← 直接回车
# 生成完毕,你什么也看不到是正常的,密钥已经存好了
查看你的公钥
bash
# 💻 Git Bash 中执行
cat ~/.ssh/id_ed25519.pub
# 会输出一大串文字,类似这样:
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your-email@example.com
# 复制整行内容(从 ssh-ed25519 开始到最后的邮箱)
三、把公钥添加到 GitHub
操作位置:🌐 浏览器
1. 打开 https://github.com/settings/keys
2. 点击 "New SSH key"
3. 填写:
- Title:随便写,比如 "我的电脑"
- Key type:保持默认 "Authentication Key"
- Key:粘贴刚才复制的那一大串公钥
4. 点击 "Add SSH key"
四、测试 SSH 连接
操作位置:💻 Git Bash
bash
ssh -T git@github.com
# 如果成功,会输出:
# Hi 你的用户名! You've successfully authenticated, but GitHub
# does not provide shell access.
# 如果失败,试一下这个:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# 然后再测试一次
ssh -T git@github.com
五、配置 Git 基础信息
操作位置:💻 Git Bash
bash
# 把下面的"你的名字"和"你的邮箱"换成你自己的
# 名字用英文或拼音,比如 zhangsan
# 邮箱用你注册 GitHub 的那个邮箱
git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"
# 一次性配好常用的设置(直接复制粘贴执行就行)
git config --global core.autocrlf true
git config --global pull.rebase true
git config --global push.default current
git config --global init.defaultBranch main
# 验证配置是否生效
git config --global user.name
git config --global user.email
# 应该分别输出你刚才设置的名字和邮箱
六、创建你的第一个 GitHub 仓库
第一步:在 GitHub 网页上创建
🌐 浏览器操作:
1. 登录 https://github.com
2. 点击右上角 "+" → "New repository"
3. 填写:
- Repository name:my-git-practice
- Description:Git 实战练习仓库
- 选择 Public(公开,方便别人看到你的贡献)
- ✅ 勾选 "Add a README file"
- Add .gitignore:选 "Node"(后面会用到)
- License:选 MIT License
4. 点击 "Create repository"
第二步:克隆到本地
bash
# 💻 Git Bash 中执行
# 先切到你想放项目的目录,比如桌面
cd /c/Users/你的Windows用户名/Desktop
# 克隆仓库(用 SSH 方式,前面配好的)
git clone git@github.com:你的GitHub用户名/my-git-practice.git
# 进入项目目录
cd my-git-practice
# 看看目录里有什么
ls -la
# 你会看到:
# .git/ ← Git 仓库数据(别动)
# .gitignore ← 忽略规则
# README.md ← 项目说明
第三步:创建文件并推送到 GitHub
bash
# 💻 Git Bash 中执行,全部在项目目录里操作
# 创建一个测试文件
echo "Hello, Git!" > hello.txt
# 把文件添加到暂存区(告诉 Git "我要提交这个文件")
git add hello.txt
# 提交到本地仓库(-m 后面是提交说明)
git commit -m "feat: add hello.txt"
# 推送到 GitHub
git push -u origin main
# 现在去 🌐 浏览器刷新你的 GitHub 仓库页面
# 你会看到 hello.txt 出现了!
七、配置文件说明
.gitignore 是什么?
🌐 浏览器 or 💻 VS Code 中查看:
.gitignore 告诉 Git "哪些文件不需要提交"
比如 node_modules/、.env(密码文件)、IDE 配置等
常用 .gitignore 规则
gitignore
# 在 💻 VS Code 或任意编辑器中编辑 .gitignore 文件
# 忽略所有 .log 文件
*.log
# 忽略 node_modules 目录
node_modules/
# 忽略 IDE 配置
.idea/
.vscode/
# 忽略环境变量文件(里面有密码!)
.env
.env.local
八、SSH 多账号配置(可选)
如果你有个人 GitHub 和公司 GitHub 两个账号
操作位置:💻 Git Bash
bash
# 生成第二个密钥(公司账号用)
ssh-keygen -t ed25519 -C "你的公司邮箱@chinaunicom.cn" -f ~/.ssh/id_ed25519_work
# 编辑 SSH 配置文件
cat > ~/.ssh/config << 'EOF'
# 个人账号
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# 公司账号
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
EOF
# 使用时:
# 克隆个人仓库:git clone git@github.com-personal:你的用户名/repo.git
# 克隆公司仓库:git clone git@github.com-work:公司名/repo.git
九、练习清单
按顺序完成以下操作:
- 🌐 在浏览器注册 GitHub 账号
- 🌐 开启两步验证(2FA)
- 💻 在 Git Bash 生成 SSH 密钥
- 🌐 在浏览器把公钥添加到 GitHub
- 💻 在 Git Bash 测试 SSH 连接
- 💻 在 Git Bash 配置 user.name 和 user.email
- 🌐 在浏览器创建仓库 my-git-practice
- 💻 在 Git Bash 克隆仓库并 push 一个文件
下一章 :02-GitHub仓库管理与Issue