前言

写代码不用 Git 等于走钢丝不系保险绳。这篇把 Git 的核心心智模型和日常高频操作整理成册,附"事故急救"章节。

一、心智模型:三个区

工作区 (Working Directory)
   │  git add
   ▼
暂存区 (Staging / Index)
   │  git commit
   ▼
本地仓库 (Repository)
   │  git push
   ▼
远程仓库 (Remote: GitHub/Gitee/自建)

记住这个流向,所有命令都能推导出来。

二、基础操作流

# 初始化 / 克隆
git init
git clone https://gitee.com/user/repo.git

# 身份配置(第一次必做)
git config --global user.name "zy"
git config --global user.email "zy@example.com"

# 日常四连
git status                 # 看状态(先看再动, 好习惯)
git add file.txt           # 加入暂存区; git add . 全部
git commit -m "feat: add login"   # 提交到本地仓库
git push origin main       # 推到远程

# 拉取更新
git pull = git fetch + git merge

写好 commit message:推荐约定式提交——feat: 新功能、fix: 修 bug、docs: 文档、refactor: 重构、chore: 杂务。

三、查看历史与差异

git log --oneline --graph --all    # 一图流看历史
git diff                           # 工作区 vs 暂存区
git diff --staged                  # 暂存区 vs 最近提交
git diff HEAD~1                    # 与上一次提交比
git show 3f2a9c1                   # 看某次提交改了什么
git blame file.py                  # 这行代码是谁写的(甩锅神器)

四、分支操作

git branch                  # 列分支
git switch -c feature/login # 创建并切换新分支(新命令)
git switch main             # 切回主分支
git merge feature/login     # 把 feature 合进当前分支
git branch -d feature/login # 删除已合并分支

# 远程分支
git fetch origin
git switch -c hotfix origin/hotfix    # 基于远程分支建本地分支
git push origin --delete old-branch   # 删远程分支

merge vs rebase:

# merge: 保留分叉历史, 产生合并提交
git merge main

# rebase: 把我的提交"嫁接"到 main 顶端, 历史成一条直线(整洁但改写历史)
git rebase main

团队铁律:公共分支禁 rebase(会改写别人的历史),个人 feature 分支 rebase main 保持最新是优雅实践。

五、远程仓库

git remote -v                          # 查看远程
git remote add origin <url>            # 关联远程
git push -u origin main                # 首推并建立跟踪
git remote set-url origin <new-url>    # 换远程地址

# 多远程(公司 + 个人)
git remote add gitee https://gitee.com/zy/repo.git
git push gitee main

国内访问 GitHub 慢,可以 Gitee/GitCode 做镜像,或用 SSH 协议 + 代理。

SSH 免密:

ssh-keygen -t ed25519 -C "zy@example.com"
cat ~/.ssh/id_ed25519.pub     # 贴到平台的 SSH Keys 设置里
git remote set-url origin git@github.com:zy/repo.git

六、.gitignore

# .gitignore 示例
node_modules/
*.pyc
__pycache__/
.venv/
.env                  # 密钥文件千万别进仓库!
*.log
.idea/
dist/

已被跟踪的文件加入 ignore 后需:

git rm --cached .env && git commit -m "remove env"

七、事故急救手册

事故 急救命令
改乱了工作区,想放弃修改 git restore file
add 错了想撤出暂存区 git restore --staged file
commit 信息写错 git commit --amend
少提交了文件 补 add 后 git commit --amend --no-edit
想撤销最近一次提交(保留改动) git reset --soft HEAD~1
想彻底回滚到某个提交 git reset --hard 3f2a9c1(⚠️ 丢弃之后的改动)
已经 push 的错误提交 git revert 3f2a9c1(生成反向提交, 安全)
误删分支 git reflog 找到提交号 → git switch -c rescued 3f2a9c1
stash 暂存现场 git stash / git stash pop

reset 三模式:

git reset --soft HEAD~1    # 只回退 commit, 改动留在暂存区
git reset --mixed HEAD~1   # 回退 commit+add, 改动留在工作区(默认)
git reset --hard HEAD~1    # 全部丢弃, 回到过去

八、进阶技巧

# 交互式暂存:一块一块挑着提交
git add -p

# 挑 cherry-pick:把某个提交"复制"到当前分支
git cherry-pick 3f2a9c1

# 二分查找引入 bug 的提交(超好用)
git bisect start
git bisect bad            # 当前是坏的
git bisect good v1.2      # 这个版本是好的
# Git 自动切换中间版本, 测试后标记 good/bad, 定位凶手
git bisect reset

# 储藏现场再切分支
git stash push -m "wip: login"
git stash list
git stash pop

小结

层级 命令
提交流 status → add → commit → push
分支 switch -c / merge / 个人分支 rebase
撤销 工作区 restore、提交 reset --soft、已推送 revert
救命 reflog 永远有后悔药

本文是「工具」系列第 1 篇。