git 使用手册
下面给你一份「Git 常用操作手册」(偏实战速查),按日常使用频率组织。你可以直接当作团队内部 SOP。
0. 基础概念速记
- 工作区 (Working Tree):你正在编辑的文件
- 暂存区 (Index/Stage):准备提交的“候选快照”
- 本地仓库 (Local Repo):提交历史(commit)
- 远端仓库 (Remote Repo):GitHub/GitLab 等
1. 初始化 / 克隆
########################### 克隆 ###########################
git clone <repo_url> # 克隆远端仓库
git clone <repo_url> <dir> # 克隆到指定目录
########################### 初始化 ###########################
git init # 在当前目录初始化仓库
########################### 用户信息 ###########################
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
########################### 保存账号密码 ###########################
# 启用永久存储
git config --global credential.helper store
# 执行一次 "git pull" 或 "git push"
- 在执行 git pull 或 git push 时输入用户名和密码,Git 会将凭据存储到纯文本文件中(默认位置为 ~/.git-credentials)。
# 查看保存的凭据
cat ~/.git-credentials
########################### 保存账号密码 ###########################
2. 基础查看:状态、差异、历史
git status # 看当前修改、暂存情况
git status -sb # 简洁模式(推荐)
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs HEAD(上次提交)
git diff <commit1> <commit2> # 两个提交之间差异
git diff <branchA>..<branchB> # 两分支差异(常用)
git log # 提交历史
git log --oneline --graph --decorate --all
git show <commit> # 查看某个 commit 详情
3. 添加 / 提交
git add <file> # 把文件加入暂存区
git add --all # 把所有改动加入暂存区
git add . # 把当前目录所有改动加入暂存区
git add -p # 交互式选择部分改动加入(强烈推荐)
git commit -m "message" # 提交暂存区
git commit -am "message" # 仅对“已跟踪文件”自动 add + commit(不会包含新文件)
git commit --amend # 修改最近一次提交(补漏/改 message)
amend 改了提交哈希,若已 push 到远端,通常需要
--force-with-lease(见后面)。
4. 分支:创建、切换、删除、重命名
git branch # 列本地分支
git branch -a # 列本地+远端分支
git branch -vv # 显示 upstream 跟踪信息
git switch <branch> # 切换分支(推荐)
git switch -c <branch> # 创建并切换
# 旧命令也常见:git checkout <branch>
git branch -d <branch> # 删除分支(已合并)
git branch -D <branch> # 强制删除
git branch -m <new> # 重命名当前分支
git branch -m <old> <new> # 重命名指定分支
5. 远端:push / pull / fetch
git remote -v # 查看远端
git remote add origin <url> # 添加远端
git fetch # 拉取远端引用(不合并)
git pull # = fetch + merge(或 rebase,取决于配置)
git pull --rebase # 用 rebase 方式同步(更干净的线性历史)
git push # 推送当前分支到 upstream
git push -u origin <branch> # 首次推送并设置 upstream
git push origin --delete <branch> # 删除远端分支
推荐配置(减少踩坑)
git config --global pull.rebase true # pull 默认 rebase(可选)
git config --global fetch.prune true # fetch 自动清理远端已删除的分支引用
6. 合并与变基:merge / rebase
merge(保留分叉历史)
git merge <branch>
rebase(把你的提交“挪到”新基底上)
git rebase <branch> # 将当前分支变基到 branch
git rebase --continue
git rebase --abort
交互式 rebase(整理提交历史)
git rebase -i HEAD~5 # 整理最近 5 个提交(squash/fixup/reword)
7. 冲突处理(通用流程)
git status # 找到冲突文件
# 手动编辑解决冲突(<<<<<< ====== >>>>>>)
git add <file> # 标记已解决
git commit # merge 冲突时需要 commit
# 或 rebase 场景:
git rebase --continue
8. 撤销与回退(最常用、最易混)
8.1 撤销工作区改动(未 add)
git restore <file> # 丢弃工作区改动
git restore . # 丢弃全部工作区改动
8.2 撤销暂存区(已 add 但未 commit)
git restore --staged <file> # 从暂存区移出,保留工作区改动
8.3 回退提交
- 保留代码、只回退提交(推荐):用 revert(生成一个“反向提交”,安全)
git revert <commit>
git revert HEAD # 回滚最近一次提交
- 重写历史(危险,慎用):reset(会移动 HEAD)
git reset --soft HEAD~1 # 回退 commit,保留暂存区
git reset --mixed HEAD~1 # 回退 commit,清空暂存区(默认)
git reset --hard HEAD~1 # 回退 commit,丢弃所有改动(危险)
已 push 到远端的提交,尽量用
revert。确需强推用--force-with-lease。
9. Stash:临时保存现场
git stash # 保存当前工作区+暂存区
git stash -u # 包含未跟踪文件
git stash list
git stash show -p stash@{0} # 看 stash 内容
git stash pop # 应用并删除
git stash apply # 应用不删除
git stash drop stash@{0}
git stash clear
10. 文件操作:删除/移动/忽略
git rm <file> # 删除并纳入版本控制
git rm --cached <file> # 停止跟踪但保留文件(常配 .gitignore)
git mv <old> <new> # 移动/重命名
########################### 删除remote文件并添加ignore ###########################
# 1 从 Git 暂存区中移除文件,告诉 Git 停止跟踪这个文件,但不要从你的本地电脑磁盘上删除它
git rm --cached package-lock.json
# 2 将此更改提交并推送到 GitHub
git commit -m "从版本控制中移除 package-lock.json"
git push # 也可以用 git push origin <你的分支名称>
# 3 永久忽略该文件
## 将 package-lock.json 加入 .gitignore
########################### 删除remote文件并添加ignore ###########################
.gitignore 常见示例:
/vendor/
/node_modules/
.env
.DS_Store
*.log
11. Tag:发布版本
git tag # 列 tag
git tag v1.2.3 # 轻量 tag
git tag -a v1.2.3 -m "release" # 注释 tag(推荐)
git push origin v1.2.3
git push origin --tags
12. 常用排查/救命工具
git blame <file> # 谁改了哪一行
git reflog # 找回“误 reset/rebase”丢失的提交(救命)
git bisect # 二分定位引入 bug 的提交(进阶)
13. 强推的正确姿势(避免误伤)
git push --force-with-lease # 比 --force 更安全(推荐)
14. 推荐别名(提升效率)
git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.co "switch"
git config --global alias.br "branch -vv"
git config --global alias.df "diff"
15. 高频场景 SOP(直接照做)
15.1 开发一个功能分支并提 PR
git switch main
git pull --rebase
git switch -c feat/xxx
# coding...
git add -p
git commit -m "feat: xxx"
git push -u origin feat/xxx
15.2 同步主分支最新代码到功能分支(保持线性历史)
git fetch
git switch feat/xxx
git rebase origin/main
# 有冲突:解决 -> git add -> git rebase --continue
git push --force-with-lease
15.3 误删/误回退后找回提交
git reflog
git reset --hard <hash_from_reflog>
如果你愿意,我也可以按你团队实际习惯(GitFlow / trunk-based、是否要求 squash、是否允许 merge commit、是否强制 rebase、CI 规则等)把这份手册改成“你们公司的标准流程版”(含分支命名规范、提交规范、发布 tag/版本号规范、以及冲突处理准则)。