Git 和 GitHub 初学者指南:友好指南
发布: (2026年1月18日 GMT+8 04:37)
3 min read
原文: Dev.to
Source: Dev.to
安装 Git Bash
- Google Git Bash
- 下载 Windows 安装程序
- 使用以下推荐设置运行安装程序:
- 选择 “仅在 Git Bash 中使用 Git”
将 Git 连接到你的 GitHub 账户
检查 Git 版本
git --version
配置你的身份信息
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
生成 SSH 密钥
# Generate a new SSH key
ssh-keygen -t ed25519 -C "you@example.com"
公钥将保存到:
C:\Users\YOUR_USERNAME\.ssh\id_ed25519.pub
将 SSH 密钥添加到 GitHub
- 前往你的 GitHub 账户 → Settings → SSH and GPG keys。
- 点击 “New SSH key”,粘贴
id_ed25519.pub的内容并保存。
测试连接
ssh -T git@github.com
你应该会看到一条成功信息,确认已通过身份验证。
什么是 Git,为什么版本控制很重要?
Git 是一个免费、开源的分布式版本控制系统(DVCS),开发者用它来在软件开发过程中跟踪源代码和其他文件的更改。它使多个人能够在同一项目上协作而不会相互覆盖工作,并且在需要时允许用户恢复到之前的版本。
版本控制的重要性
- 保留完整的更改历史。
- 促进团队成员之间的协作。
- 支持分支和合并,以进行功能开发。
- 为恢复丢失的工作提供安全网。
如何将代码推送到 GitHub
# Initialize a new repository (if you haven’t already)
git init
# Add your files to Git’s staging area
git add .
# Commit your changes with a message
git commit -m "Initial commit"
# Connect your project to GitHub
git remote add origin https://github.com/username/repository.git
# Push your code to the remote repository
git push -u origin main
如何从 GitHub 拉取代码
拉取会将 GitHub 上的最新更改下载到本地机器:
git pull origin main
使用 Git 跟踪更改
-
Status(状态): 显示哪些文件是新建、已修改或已暂存。
git status -
Log(日志): 查看带有提交信息、作者和时间戳的提交历史。
git log -
Diff(差异): 在提交前比较更改;显示具体添加或删除的行。
git diff