适用人群:所有开发者(零基础即可)
学习时长:约1-2天(每天2小时)
工具:Git CLI + GitHub/Gitee
重要程度:★★★★★(必学)
一、Git 是什么?
Git 是全球最流行的分布式版本控制系统,用于跟踪代码变更、多人协作开发。
| 概念 | 说明 |
|---|---|
| 仓库(Repository) | 项目代码的存储空间 |
| 提交(Commit) | 一次代码变更的快照 |
| 分支(Branch) | 独立的开发线路 |
| 合并(Merge) | 将分支代码合并到主线 |
| 远程(Remote) | 远程仓库(GitHub/Gitee) |
二、安装与配置
# ====================
# 安装
# ====================
# macOS
brew install git
# Ubuntu
sudo apt install git
# Windows
# 下载:https://git-scm.com/download/win
# ====================
# 配置(必须)
# ====================
git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"
# 查看配置
git config --list
# ====================
# 生成SSH密钥(连接GitHub)
# ====================
ssh-keygen -t ed25519 -C "你的邮箱@example.com"
# 一路回车
# 查看公钥
cat ~/.ssh/id_ed25519.pub
# 将公钥添加到 GitHub → Settings → SSH Keys
三、基本工作流
3.1 创建仓库
# 方式1:本地创建
mkdir myproject
cd myproject
git init
# 方式2:克隆远程仓库
git clone https://github.com/username/repo.git
git clone git@github.com:username/repo.git # SSH方式
3.2 日常操作
# ====================
# 查看状态
# ====================
git status # 查看工作区状态
git diff # 查看未暂存的更改
git diff --staged # 查看已暂存的更改
git log --oneline # 查看提交历史(简洁)
git log --oneline --graph # 查看分支图
# ====================
# 添加到暂存区
# ====================
git add file.txt # 添加单个文件
git add . # 添加所有更改
git add *.js # 添加所有JS文件
git add src/ # 添加src目录
# ====================
# 提交
# ====================
git commit -m "feat: 添加用户注册功能" # 简短描述
git commit -am "fix: 修复登录bug" # 跳过add,直接提交已跟踪文件
# ====================
# 推送到远程
# ====================
git push origin main # 推送到远程main分支
git push # 推送当前分支
# ====================
# 拉取远程更新
# ====================
git pull origin main # 拉取并合并
git pull # 拉取当前分支
3.3 提交信息规范
feat: 新功能
fix: 修复bug
docs: 文档更新
style: 代码格式(不影响功能)
refactor: 重构
test: 测试相关
chore: 构建/工具变更
示例:
feat: 添加用户登录功能
fix: 修复订单金额计算错误
docs: 更新API文档
refactor: 重构用户服务层
四、分支管理
# ====================
# 分支操作
# ====================
git branch # 查看本地分支
git branch -a # 查看所有分支(含远程)
git branch feature/login # 创建分支
git checkout feature/login # 切换分支
git checkout -b feature/login # 创建并切换
# ====================
# 合并分支
# ====================
git checkout main # 切换到主分支
git merge feature/login # 合并feature分支
# ====================
# 删除分支
# ====================
git branch -d feature/login # 删除已合并的分支
git branch -D feature/login # 强制删除
git push origin --delete feature/login # 删除远程分支
# ====================
# 分支管理策略
# ====================
# main:生产环境代码,始终可部署
# develop:开发分支,集成各feature
# feature/xxx:功能分支,从develop创建
# hotfix/xxx:紧急修复,从main创建
分支工作流示例
# 开发新功能
git checkout develop
git checkout -b feature/user-profile
# ... 编写代码 ...
git add .
git commit -m "feat: 添加用户个人资料页面"
git checkout develop
git merge feature/user-profile
git push origin develop
# 紧急修复
git checkout main
git checkout -b hotfix/login-bug
# ... 修复代码 ...
git add .
git commit -m "fix: 修复登录token过期问题"
git checkout main
git merge hotfix/login-bug
git push origin main
五、远程仓库
# ====================
# 远程操作
# ====================
git remote # 查看远程
git remote -v # 查看远程URL
git remote add origin https://github.com/user/repo.git # 添加远程
git remote set-url origin https://github.com/user/new-repo.git # 修改远程
# ====================
# 推送
# ====================
git push -u origin main # 首次推送,设置上游
git push # 后续推送
# ====================
# 拉取
# ====================
git fetch origin # 获取远程更新(不合并)
git pull origin main # 获取并合并
# ====================
# 标签
# ====================
git tag v1.0.0 # 创建标签
git tag -a v1.0.0 -m "版本1.0.0" # 带注释的标签
git push origin v1.0.0 # 推送标签
git push origin --tags # 推送所有标签
六、撤销与回退
# ====================
# 撤销工作区更改
# ====================
git checkout -- file.txt # 撤销单个文件
git checkout -- . # 撤销所有更改
# ====================
# 取消暂存
# ====================
git reset HEAD file.txt # 取消add
# ====================
# 修改最后一次提交
# ====================
git commit --amend -m "新的提交信息"
# ====================
# 回退提交
# ====================
git reset --soft HEAD~1 # 回退提交,保留更改在暂存区
git reset --mixed HEAD~1 # 回退提交,保留更改在工作区
git reset --hard HEAD~1 # 回退提交,丢弃所有更改(危险!)
# ====================
# 查看操作历史
# ====================
git reflog # 查看所有操作记录
# ====================
# 从历史恢复
# ====================
git checkout abc123 -- file.txt # 从某次提交恢复文件
git revert abc123 # 创建新提交来撤销某次提交
七、.gitignore 文件
# ====================
# 常用忽略规则
# ====================
# 依赖目录
node_modules/
vendor/
__pycache__/
.venv/
# 构建输出
dist/
build/
*.o
*.pyc
# 环境配置
.env
.env.local
.env.production
# IDE配置
.idea/
.vscode/
*.swp
*.swo
# 系统文件
.DS_Store
Thumbs.db
# 日志
*.log
logs/
# 上传文件
uploads/
storage/
# 编辑器备份
*~
*.bak
八、GitHub/Gitee 协作
8.1 Fork 工作流
# 1. Fork 仓库到自己的账号
# 2. 克隆自己的仓库
git clone https://github.com/your-username/repo.git
# 3. 添加上游仓库
git remote add upstream https://github.com/original/repo.git
# 4. 保持同步
git fetch upstream
git merge upstream/main
# 5. 创建功能分支
git checkout -b feature/my-feature
# 6. 提交并推送
git add .
git commit -m "feat: 添加新功能"
git push origin feature/my-feature
# 7. 在GitHub上创建Pull Request
8.2 Pull Request 规范
标题:feat: 添加用户注册功能
描述:
## 变更内容
- 添加注册页面
- 添加注册API
- 添加邮箱验证
## 测试
- [x] 单元测试通过
- [x] 手动测试通过
## 关联Issue
Closes #123
九、常用命令速查表
| 命令 | 说明 |
|---|---|
git init | 初始化仓库 |
git clone <url> | 克隆仓库 |
git add . | 添加所有更改 |
git commit -m "msg" | 提交 |
git push | 推送 |
git pull | 拉取 |
git branch | 查看分支 |
git checkout -b <name> | 创建并切换分支 |
git merge <branch> | 合并分支 |
git status | 查看状态 |
git log --oneline | 查看历史 |
git diff | 查看更改 |
git reset --hard HEAD~1 | 回退提交 |
git stash | 暂存更改 |
git stash pop | 恢复暂存 |
学习建议
- 先学会add/commit/push,这是日常最高频的操作
- 理解分支概念,feature分支开发是标准流程
- 写好提交信息,方便回溯和团队协作
- 配置.gitignore,避免提交不必要的文件
- 多练习,Git用多了自然熟练