Skip to content

速查手册 ​

场景化速查:遇到问题直接按目录找。所有命令按"目标 → 命令"组织。

配置类 ​

bash
# 身份(必配)
git config --global user.name "名字"
git config --global user.email "邮箱"

# 查看
git config --list                     # 全部配置
git config --list --show-origin      # 显示来源文件
git config user.name                 # 单项查询

# 编辑器 / 换行符
git config --global core.editor "code --wait"
git config --global core.autocrlf true        # Windows
git config --global core.quotepath false      # 中文文件名正常显示

# 常用行为
git config --global pull.rebase true          # pull 默认 rebase
git config --global init.defaultBranch main   # 默认主分支名
git config --global push.autoSetupRemote true # push 自动设上游

# 帮助
git help <命令>          # 完整手册
git <命令> -h            # 简洁帮助

仓库创建与克隆 ​

bash
git init                              # 初始化仓库
git init -b main                      # 初始化并指定主分支名
git clone <url>                       # 克隆
git clone <url> <目录名>              # 克隆到指定目录
git clone --depth 1 <url>             # 浅克隆(只要最新历史)
git clone --branch <分支> <url>       # 克隆指定分支

日常提交 ​

bash
git status                    # 查看状态
git status -s                 # 简短状态
git add <文件>                # 暂存
git add .                     # 暂存所有
git add -p                    # 交互式分块暂存
git commit -m "信息"          # 提交
git commit -am "信息"         # 已跟踪文件 add+commit 一步(新文件不行)
git commit --amend -m "信息"  # 修改最近提交信息

查看与搜索 ​

bash
# 历史
git log --oneline                     # 一行一条
git log --oneline --graph --all --decorate   # 分支图
git log -5                            # 最近 5 条
git log -p                            # 带完整 diff
git log --stat                        # 带文件统计
git log --author="名字"               # 按作者
git log --since="2 weeks ago"         # 按时间
git log --grep="关键字"               # 按提交信息
git log -- <文件>                     # 只看某文件历史
git log -S "字符串"                   # 找增删该字符串的提交

# 差异
git diff                      # 工作区 vs 暂存区(未 add 的改动)
git diff --staged             # 暂存区 vs 版本库(将提交的内容)
git diff HEAD                 # 全部未提交改动
git diff <提交1> <提交2>      # 两个提交间
git diff --stat               # 只统计

# 查看提交
git show <哈希>               # 某提交的改动
git show HEAD                 # 最近提交
git blame <文件>              # 逐行追溯
git grep "关键字"             # 搜索跟踪文件内容

分支操作 ​

bash
git branch                        # 列出本地分支
git branch -a                     # 全部(含远程)
git branch -vv                    # 含上游跟踪信息
git branch <名字>                 # 创建
git switch <名字>                 # 切换
git switch -c <名字>              # 创建并切换
git switch -                      # 切回上一个分支
git branch -d <名字>              # 删除(已合并)
git branch -D <名字>              # 强制删除(未合并)
git branch -m <新名字>            # 重命名

合并与冲突 ​

bash
git merge <分支>                  # 合并分支
git merge --no-ff <分支>          # 强制生成合并提交
git merge --abort                 # 放弃合并
git merge --squash <分支>         # 压缩合并

# 冲突解决流程
git status                        # ① 看冲突文件
git diff                          # ② 看冲突详情
# ③ 编辑文件,删除 <<<<<<< ======= >>>>>>> 标记
git add <文件>                    # ④ 标记已解决
git commit                        # ⑤ 完成合并

远程协作 ​

bash
git remote -v                     # 查看远程
git remote add origin <url>       # 添加远程
git remote set-url origin <url>   # 改地址
git remote remove origin          # 移除远程

git fetch                         # 只下载远程更新
git fetch --prune                 # 下载并清理已删除的远程分支引用
git pull                          # fetch + merge
git pull --rebase                 # fetch + rebase
git push                          # 推送
git push -u origin <分支>         # 首次推送并设上游
git push origin --delete <分支>   # 删除远程分支
git push --tags                   # 推送所有标签
git push --force-with-lease <分支>  # 安全强推(远程没被别人动过才推)

撤销与回滚 ​

bash
# 工作区(未 add)
git restore <文件>                # 丢弃未暂存修改(⚠️不可恢复)
git restore .                     # 丢弃全部

# 暂存区
git restore --staged <文件>       # 撤销暂存(保留工作区修改)

# 提交历史(未推送,可安全用 reset)
git reset --soft HEAD~1           # 撤销提交,改动留在暂存区
git reset HEAD~1                  # 撤销提交,改动回工作区
git reset --hard HEAD~1           # 撤销提交并丢弃改动(⚠️)
git reset --hard <哈希>           # 回到任意提交

# 提交历史(已推送,用 revert)
git revert <哈希>                 # 生成反向提交回滚(安全)

# 找回
git reflog                        # 查看所有 HEAD 移动历史
git reset --hard <哈希>           # 找回丢失的提交

# 临时保存
git stash                         # 保存未提交改动
git stash list                    # 列表
git stash pop                     # 恢复最近一个
git stash apply stash@{1}         # 恢复指定
git stash drop stash@{0}          # 删除指定
git stash -u                      # 连未跟踪文件一起存

标签与发布 ​

bash
git tag                           # 列出
git tag v1.0.0                    # 轻量标签
git tag -a v1.0.0 -m "说明"       # 附注标签
git tag -a v1.0.0 <哈希>          # 给历史提交打标签
git push origin v1.0.0            # 推送单个
git push origin --tags            # 推送全部
git tag -d v1.0.0                 # 删除本地
git push origin :refs/tags/v1.0.0 # 删除远程
git checkout v1.0.0               # 检出标签(detached HEAD)

高级与排障 ​

bash
# 重写历史
git rebase <分支>                 # 变基
git pull --rebase                 # 拉取并变基
git rebase -i HEAD~3              # 交互式整理最近 3 个提交
git rebase --continue             # 冲突解决后继续
git rebase --abort                # 放弃变基
git cherry-pick <哈希>            # 移植某个提交

# 定位问题
git bisect start                  # 二分查找坏提交
git bisect bad                    # 标记坏的
git bisect good <哈希>            # 标记好的
git bisect reset                  # 结束
git bisect run <命令>             # 自动判断

# 清理
git clean -n                      # 预览未跟踪文件(务必先看)
git clean -fd                     # 删除未跟踪文件/目录(⚠️不可恢复)
git gc                            # 垃圾回收与打包

# 子模块 / 多工作区
git submodule add <url> <路径>
git worktree add <路径> <分支>

# 仓库体检
git fsck                          # 检查对象完整性
git count-objects -v              # 对象统计

状态符号速查 ​

git status -s 两列:左列 = 暂存区,右列 = 工作区。

符号含义
??未跟踪(Untracked)
A已暂存新增
M已修改
D已删除
R已重命名
U冲突未解决
MM暂存区和工作区都有修改

常见问题速查(FAQ) ​

Q1:push 被拒绝 non-fast-forward ​

bash
git pull --rebase && git push

Q2:commit 写错信息,还没 push ​

bash
git commit --amend -m "正确信息"

Q3:add 了不该 add 的文件 ​

bash
git restore --staged <文件>

Q4:文件被误删/改坏,想恢复 ​

bash
git restore <文件>                    # 未提交的修改
git checkout <哈希> -- <文件>         # 从某次提交恢复某个文件

Q5:上次提交少了一个文件 ​

bash
git add <漏掉的文件>
git commit --amend --no-edit

Q6:reset/rebase 后反悔了 ​

bash
git reflog
git reset --hard <找回的哈希>

Q7:误提交了密钥/大文件 ​

bash
git rm --cached <文件>                # 移出跟踪(保留本地)
echo "文件名" >> .gitignore
git commit -m "chore: 移除误提交的密钥"
# ⚠️ 密钥已推送则必须去远程平台轮换密钥!历史里的密钥要用
# git filter-repo / BFG 等工具彻底清除

Q8:分支和 main 差太远,冲突很多 ​

bash
git switch <分支>
git rebase main        # 尽早、频繁地把 main 的更新变基进来,冲突早解决

Q9:想丢弃本地所有未提交改动(含新文件) ​

bash
git reset --hard HEAD
git clean -fd

(⚠️ 两者都不可恢复,先想清楚)

Q10:只想下载远程代码看看,不合并 ​

bash
git fetch
git show origin/main:path/to/file

Q11:不同仓库之间拷贝某个提交 ​

bash
# 在源仓库
git format-patch -1 <哈希> -o /tmp/patches
# 在目标仓库
git am /tmp/patches/0001-*.patch

(或直接 git cherry-pick,如果两仓库同源)

Q12:git pull 时"Please commit or stash"错误 ​

工作区有未提交的修改挡住了合并:

bash
git stash        # 先存起来
git pull
git stash pop    # 再恢复

推荐别名配置 ​

bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.sw switch
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.la "log --oneline --graph --all --decorate --stat"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.unstage "restore --staged"
git config --global alias.df diff
git config --global alias.dfs "diff --staged"