Git 入门教程——从零掌握版本控制
作者: CaoZH
日期: 2023-10-15
本文为原创教程
Git 是当今最流行的版本控制系统。无论你写什么代码——前端、后端、脚本还是文档——Git 都是必修课。本文从零开始,带你掌握日常开发中最常用的 Git 操作。
一、安装与配置
安装 Git
1 2 3 4 5 6 7 8 9 10 11
| sudo apt install -y git
sudo yum install -y git
brew install git
git --version
|
首次配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git config --global user.name "Your Name" git config --global user.email "your@email.com"
git config --global init.defaultBranch main
git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.lg "log --oneline --graph --all --decorate"
git config --list
|
二、核心概念
1 2 3 4 5 6 7 8 9 10 11 12 13
| 工作区(Working Directory) 暂存区(Staging Area) 本地仓库(Local Repo) │ │ │ │ git add │ git commit │ ├──────────────────────────►├───────────────────────►│ │ │ │ │◄───────────────────────────┤◄───────────────────────┤ │ git checkout │ git reset │ │ │ │ │ │ │ │ git push │ ├───────────► 远程仓库 │◄───────────────────────┤ │ git pull │
|
三、基本操作
初始化仓库
1 2 3 4 5 6 7
| mkdir my-project && cd my-project git init
git clone https://github.com/user/repo.git git clone git@github.com:user/repo.git
|
日常三连
1 2 3 4 5 6 7 8 9 10 11 12
| git status
git add index.html git add src/ git add . git add -p
git commit -m "feat: 添加用户登录功能" git commit -a -m "fix: 修复空指针"
|
四、分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| git branch git branch -a git branch -r
git branch feature-login git checkout -b feature-login
git checkout main git switch main
git checkout main git merge feature-login
git branch -d feature-login git branch -D feature-login git push origin --delete feature-login
|
完整的分支工作流示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| git checkout main git pull origin main
git checkout -b feature/user-management
git add . git commit -m "feat: 添加用户CRUD接口"
git push origin feature/user-management
git checkout main git pull origin main
git branch -d feature/user-management
|
五、远程仓库操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git remote -v
git remote add origin https://github.com/user/repo.git
git push origin main git push -u origin feature/login git push --all
git pull origin main git fetch origin
git remote show origin
|
六、撤销与回退
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git checkout -- index.html git restore index.html git restore .
git reset HEAD index.html git restore --staged index.html git reset HEAD .
git commit --amend -m "新消息" git reset --soft HEAD~1 git reset --hard HEAD~1
git revert HEAD git revert HEAD~3..HEAD
|
七、解决冲突
当合并分支时,如果两个人改了同一文件的同一区域,就会产生冲突。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git merge feature/login
git status
git add src/app.js git commit -m "fix: 解决登录功能冲突"
|
八、查看历史
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git log git log --oneline git log --graph git log --oneline --graph --all
git log -p index.html git blame index.html
git log --grep="fix" git log --author="CaoZH" git log --since="2023-01-01"
|
九、.gitignore
忽略不需要跟踪的文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| # .gitignore
# 依赖 node_modules/ vendor/ venv/
# 构建产物 dist/ build/ target/ *.jar
# 环境配置 .env .env.local
# IDE .idea/ .vscode/ *.swp
# 系统文件 .DS_Store Thumbs.db
# 日志 *.log logs/
|
十、总结
Git 其实不需要背命令,掌握这 15 个操作就能覆盖 90% 的日常需求:
| 场景 |
命令 |
| 克隆仓库 |
git clone |
| 查看状态 |
git status |
| 添加到暂存 |
git add |
| 提交 |
git commit -m "..." |
| 推送 |
git push |
| 拉取 |
git pull |
| 创建分支 |
git checkout -b |
| 合并 |
git merge |
| 查看历史 |
git log --oneline --graph |
| 撤销修改 |
git restore |
| 回退提交 |
git reset |
| 解决冲突 |
手动编辑 → git add → git commit |
| 暂存修改 |
git stash |
| 还原暂存 |
git stash pop |
| 对比差异 |
git diff |
首发于 CaoZH 的笔记