Git 是一个功能极其强大的分布式版本控制系统,支持超过 150 种命令和无数种工作流组合。下面我将按照 操作目标 和 功能领域 为你详细列举 Git 能够完成的所有操作类别,每个类别下说明具体能做什么,并附上典型命令示例。
一、仓库生命周期管理
1. 创建仓库
- 在当前目录初始化一个新仓库:
git init - 在指定目录初始化:
git init <directory> - 初始化一个裸仓库(用作中央服务器):
git init --bare - 从远程克隆已有仓库:
git clone <url> - 浅克隆(只拉取最近几次提交):
git clone --depth 1 <url> - 克隆指定分支:
git clone -b <branch> <url>
2. 仓库配置
- 设置用户名/邮箱(全局/仓库级):
git config [--global] user.name/email - 设置别名:
git config --global alias.<alias> <command> - 设置默认编辑器:
git config core.editor <editor> - 查看所有配置:
git config --list - 设置 CRLF 换行符转换:
git config core.autocrlf true - 启用彩色输出:
git config color.ui auto - 设置差异工具:
git config diff.tool <tool>
3. 仓库信息查看
- 查看仓库状态:
git status(支持简洁模式-s) - 查看仓库摘要信息:
git summary - 查看 Git 版本:
git --version
二、核心快照操作(日常提交)
1. 添加文件到暂存区
- 添加单个文件:
git add <file> - 添加所有变更(包括新增、修改、删除):
git add -A或git add . - 只添加已跟踪文件的修改:
git add -u - 交互式添加(选择部分修改):
git add -i - 添加所有
*.js文件:git add *.js - 添加但忽略某些模式:
git add --ignore-removal .
2. 提交到本地仓库
- 普通提交:
git commit -m "<message>" - 跳过暂存区直接提交已跟踪文件:
git commit -am "<message>" - 修改最近一次提交(信息或内容):
git commit --amend - 修改提交信息但不改内容:
git commit --amend -m "<new message>" - 向最近一次提交追加文件:
git add <file>+git commit --amend --no-edit - 空提交(用于触发 CI):
git commit --allow-empty -m "trigger CI"
3. 从版本库删除文件
- 删除文件并自动暂存删除操作:
git rm <file> - 从暂存区删除但保留工作区文件(停止跟踪):
git rm --cached <file> - 递归删除目录:
git rm -r <dir> - 强制删除(即使有本地修改):
git rm -f <file>
4. 移动/重命名文件
- 重命名文件并自动暂存:
git mv <old> <new> - 移动文件到子目录:
git mv <file> <dir>/
三、分支管理
1. 分支创建与切换
- 查看本地分支:
git branch - 查看所有分支(含远程):
git branch -a - 查看远程分支:
git branch -r - 创建新分支:
git branch <branch> - 切换到已有分支:
git checkout <branch>或git switch <branch> - 创建并切换到新分支:
git checkout -b <branch>或git switch -c <branch> - 切换到上一个分支:
git checkout -或git switch -
2. 分支合并
- 合并指定分支到当前分支:
git merge <branch> - 禁用快进合并(保留分支线):
git merge --no-ff <branch> - 压缩合并(将多个提交合并为一个):
git merge --squash <branch> - 合并但不要自动提交:
git merge --no-commit <branch> - 放弃正在进行的合并:
git merge --abort - 使用
--ff-only只允许快进合并:git merge --ff-only <branch>
3. 分支删除
- 删除已合并的分支:
git branch -d <branch> - 强制删除未合并的分支:
git branch -D <branch> - 删除远程分支:
git push origin --delete <branch>或git branch -dr <remote/branch>
4. 分支重命名
- 重命名当前分支:
git branch -m <new-name> - 重命名任意分支:
git branch -m <old> <new>
5. 分支比较
- 查看两个分支的差异:
git diff <branch1>..<branch2> - 查看两个分支的共同祖先:
git merge-base <branch1> <branch2> - 列出在 A 分支但不在 B 分支的提交:
git log A ^B
6. 分支与远程同步
- 拉取远程分支并创建本地跟踪分支:
git checkout -b <branch> origin/<branch> - 设置上游分支:
git branch -u origin/<branch> - 查看跟踪关系:
git branch -vv
四、远程仓库协作
1. 远程仓库管理
- 添加远程仓库:
git remote add <name> <url> - 查看远程仓库列表:
git remote - 查看远程仓库详细信息:
git remote show <name> - 修改远程仓库地址:
git remote set-url <name> <new-url> - 删除远程仓库:
git remote remove <name> - 重命名远程仓库:
git remote rename <old> <new> - 添加多个远程源(如同时 push 到 GitHub 和 GitLab):
git remote set-url --add <name> <url>
2. 获取远程更新
- 获取远程所有分支更新(不合并):
git fetch <remote> - 获取特定分支:
git fetch <remote> <branch> - 获取并删除远程已不存在的引用:
git fetch --prune - 获取所有远程:
git fetch --all - 获取标签:
git fetch --tags
3. 拉取并合并
- 拉取并合并远程分支:
git pull <remote> <branch> - 使用 rebase 代替 merge:
git pull --rebase - 拉取但不要自动提交:
git pull --no-commit - 拉取所有分支:
git pull --all
4. 推送到远程
- 推送到同名远程分支:
git push <remote> <branch> - 首次推送并设置上游:
git push -u <remote> <branch> - 推送所有本地分支:
git push --all <remote> - 推送标签:
git push <remote> <tagname> - 推送所有标签:
git push --tags - 删除远程分支:
git push <remote> --delete <branch> - 强制推送(覆盖远程历史):
git push --force <remote> - 更安全的强制推送:
git push --force-with-lease - 推送并设置上游:
git push -u origin HEAD
5. 远程分支同步
- 修剪远程跟踪分支(删除本地已不存在的远程分支引用):
git remote prune <remote> - 同步所有远程分支状态:
git remote update
五、撤销与恢复操作
1. 工作区撤销(未暂存)
- 丢弃单个文件的修改:
git restore <file>或git checkout -- <file> - 丢弃所有工作区修改:
git restore . - 删除未跟踪的文件/目录:
git clean -fd - 预览将被删除的未跟踪文件:
git clean -n - 删除被
.gitignore忽略的文件:git clean -fx
2. 暂存区撤销(已 add 但未 commit)
- 将文件从暂存区移回工作区:
git restore --staged <file>或git reset HEAD <file> - 撤销所有暂存区的修改:
git reset - 取消跟踪文件但保留内容:
git rm --cached <file>
3. 提交撤销(已 commit)
- 软撤销 (保留修改在暂存区):
git reset --soft HEAD~1 - 混合撤销 (保留修改在工作区):
git reset --mixed HEAD~1或git reset HEAD~1 - 硬撤销 (彻底删除提交和修改):
git reset --hard HEAD~1 - 撤销到任意提交:
git reset --hard <commit> - 安全撤销(生成反向提交):
git revert <commit> - 撤销最近一次提交并生成 revert 提交:
git revert HEAD - 撤销一个区间内的提交:
git revert <oldest>..<newest> - 修改最近一次提交:
git commit --amend
4. Stash 暂存(临时保存)
- 暂存当前所有修改:
git stash - 暂存并添加说明:
git stash push -m "message" - 暂存未跟踪的文件:
git stash -u - 查看 stash 列表:
git stash list - 应用最近的 stash(保留 stash):
git stash apply - 应用并删除 stash:
git stash pop - 应用指定的 stash:
git stash apply stash@{2} - 删除指定的 stash:
git stash drop stash@{1} - 删除所有 stash:
git stash clear - 从 stash 创建新分支:
git stash branch <branch>
5. 引用日志恢复(救急)
- 查看所有 HEAD 移动记录:
git reflog - 查看特定分支的 reflog:
git reflog show <branch> - 通过 reflog 找回丢失的提交:
git checkout <hash>或git reset --hard <hash> - 清理 reflog 中过期的记录:
git reflog expire --expire=now --all
六、查看与比较
1. 查看提交历史
- 完整历史:
git log - 一行简洁显示:
git log --oneline - 图形化显示:
git log --graph --all --oneline - 查看最近 n 次提交:
git log -n 5 - 按作者过滤:
git log --author="name" - 按提交信息过滤:
git log --grep="fix" - 按时间过滤:
git log --since="2 weeks ago"或--until - 查看某个文件的修改历史:
git log --follow <file> - 查看每次提交的差异:
git log -p - 查看简洁统计:
git log --stat - 查看某个分支独有的提交:
git log <branch1> ^<branch2>
2. 查看差异
- 工作区 vs 暂存区:
git diff - 暂存区 vs 版本库:
git diff --staged或git diff --cached - 工作区 vs 最新提交:
git diff HEAD - 比较两个提交:
git diff <commit1> <commit2> - 比较两个分支:
git diff <branch1>..<branch2> - 查看某个文件的差异:
git diff <file> - 仅显示文件名:
git diff --name-only - 忽略空白差异:
git diff -w - 查看两个分支的共同祖先之后的差异:
git diff <branch1>...<branch2>(三点语法)
3. 查看提交详情
- 查看最新提交详情:
git show - 查看指定提交详情:
git show <commit> - 查看提交中某个文件的内容:
git show <commit>:<file> - 查看提交的简洁统计:
git show --stat
4. 查看文件归属
- 查看文件每行最后修改信息:
git blame <file> - 查看指定行范围:
git blame -L 10,20 <file> - 忽略空白改动:
git blame -w - 显示作者邮箱:
git blame -e
5. 搜索
- 在版本库中搜索文本:
git grep "<pattern>" - 搜索并显示行号:
git grep -n "<pattern>" - 搜索并显示文件名和匹配数:
git grep -c "<pattern>" - 在指定分支搜索:
git grep "<pattern>" <branch> - 搜索提交历史中的字符串:
git log -S "<string>"(pickaxe)
6. 统计信息
- 查看提交统计(按作者):
git shortlog -sn - 查看代码行数统计:
git ls-files | xargs wc -l - 查看仓库总提交数:
git rev-list --count HEAD - 查看贡献者列表:
git log --format='%aN' | sort -u
七、高级操作
1. 变基(Rebase)
- 将当前分支变基到目标分支:
git rebase <base-branch> - 交互式变基:
git rebase -i HEAD~n - 继续变基(解决冲突后):
git rebase --continue - 跳过当前提交:
git rebase --skip - 放弃变基:
git rebase --abort - 变基时自动整理:
git rebase --autosquash
2. 挑选提交(Cherry-pick)
- 应用一个提交到当前分支:
git cherry-pick <commit> - 应用多个提交:
git cherry-pick <commit1> <commit2> - 应用区间提交:
git cherry-pick <start>..<end> - 不自动提交:
git cherry-pick -n <commit> - 继续/放弃:
git cherry-pick --continue/--abort
3. 二分查找定位 bug
- 开始二分查找:
git bisect start - 标记当前版本为坏:
git bisect bad - 标记已知好版本:
git bisect good <commit> - 标记测试结果后 Git 自动切换:
git bisect good/bad - 结束二分查找:
git bisect reset - 自动化二分查找:
git bisect run <script>
4. 子模块管理
- 添加子模块:
git submodule add <url> <path> - 初始化子模块:
git submodule init - 更新子模块:
git submodule update - 递归更新所有子模块:
git submodule update --init --recursive - 查看子模块状态:
git submodule status - 同步子模块 URL 变更:
git submodule sync - 批量拉取子模块更新:
git submodule foreach git pull
5. 补丁与归档
- 生成补丁:
git format-patch -n <commit> - 应用补丁:
git am <patch-file> - 生成单个补丁文件:
git diff > patch.diff - 应用普通 diff 补丁:
git apply patch.diff - 导出代码快照:
git archive --format=zip HEAD > code.zip - 导出指定目录:
git archive HEAD:src/ --format=tar.gz > src.tar.gz
6. 工作树管理(多工作目录)
- 添加一个新的工作树:
git worktree add <path> <branch> - 列出所有工作树:
git worktree list - 移除工作树:
git worktree remove <path> - 清理工作树引用:
git worktree prune
7. 垃圾回收与优化
- 垃圾回收:
git gc - 激进优化:
git gc --aggressive - 自动清理:
git gc --auto - 检查仓库对象完整性:
git fsck - 重新打包:
git repack -a -d - 清理未引用的对象:
git prune
八、标签管理
1. 创建标签
- 查看所有标签:
git tag - 创建轻量标签:
git tag <tagname> - 创建附注标签(推荐):
git tag -a <tagname> -m "message" - 为历史提交打标签:
git tag -a <tagname> <commit> -m "msg" - 创建签名标签(GPG):
git tag -s <tagname> -m "msg"
2. 操作标签
- 删除本地标签:
git tag -d <tagname> - 推送单个标签:
git push origin <tagname> - 推送所有标签:
git push --tags - 删除远程标签:
git push origin --delete <tagname> - 查看标签信息:
git show <tagname> - 验证签名标签:
git tag -v <tagname>
3. 基于标签操作
- 切换到标签对应的代码:
git checkout <tagname>(会进入分离头指针状态) - 基于标签创建分支:
git checkout -b <branch> <tagname>
九、交互式工具
1. 交互式暂存
- 启动交互式 add:
git add -i - 交互式 rebase:
git rebase -i - 交互式切换分支:
git checkout -i
2. 图形化界面
- 启动 Git 自带 GUI:
git gui - 启动 Gitk 历史浏览器:
gitk - 查看分支图:
gitk --all
3. 命令自动补全
- 安装 Git 补全脚本(各系统不同)
- 启用命令别名补全
十、钩子与自动化
Git 钩子位于 .git/hooks/ 目录下,可以执行自定义脚本。
客户端钩子
pre-commit:提交前运行(检查代码风格)prepare-commit-msg:准备提交信息时commit-msg:提交信息写入前(校验格式)post-commit:提交完成后pre-rebase:变基前post-checkout:切换分支后post-merge:合并后pre-push:推送前
服务端钩子(通常用于中央仓库)
pre-receive:接收推送前update:更新分支前post-receive:接收推送后
管理钩子
- 安装钩子:将脚本放入
.git/hooks/并赋予执行权限 - 跳过钩子:
git commit --no-verify
十一、与其他系统集成
1. 与 SVN 互操作
- 克隆 SVN 仓库:
git svn clone <svn-url> - 从 SVN 获取更新:
git svn rebase - 提交到 SVN:
git svn dcommit
2. 与 Perforce 互操作
- 使用
git p4命令系列
3. 邮件工作流
- 发送补丁邮件:
git send-email - 导入邮箱中的补丁:
git am
十二、调试与诊断
1. 性能分析
- 查看 Git 命令耗时:
GIT_TRACE_PERFORMANCE=1 git <command> - 查看详细跟踪:
GIT_TRACE=1 git <command>
2. 环境变量
- 指定 Git 目录:
GIT_DIR=.git - 指定工作树:
GIT_WORK_TREE=/path - 跳过某些检查:
GIT_ALLOW_PROTOCOL=
3. 低级对象操作(Plumbing 命令)
- 查看对象类型:
git cat-file -t <hash> - 查看对象内容:
git cat-file -p <hash> - 计算文件的 Git 对象 ID:
git hash-object <file> - 写入对象到数据库:
git hash-object -w <file> - 创建树对象:
git write-tree - 创建提交对象:
git commit-tree
十三、常用场景组合操作
1. 撤销已经推送到远程的提交(安全方式)
bash
git revert HEAD
git push origin main
2. 将多个提交合并成一个
bash
git rebase -i HEAD~3
# 将后两行的 pick 改为 squash
3. 找回误删的分支
bash
git reflog
git checkout -b recovered-branch <lost-hash>
4. 清理大文件(从历史中删除)
bash
git filter-branch --tree-filter 'rm -f bigfile.zip' HEAD
# 或使用 BFG Repo-Cleaner
5. 完全重置本地仓库到远程状态
bash
git fetch origin
git reset --hard origin/main
git clean -fd
6. 备份仓库到文件
bash
git bundle create repo.bundle --all
7. 从 bundle 恢复
bash
git clone repo.bundle new-repo
总结:Git 能做的所有事情分类总览
| 类别 | 典型操作 |
|---|---|
| 仓库管理 | init, clone, config, gc, fsck |
| 快照操作 | add, commit, rm, mv, status |
| 分支管理 | branch, checkout, switch, merge, rebase |
| 远程协作 | remote, fetch, pull, push |
| 撤销恢复 | restore, reset, revert, clean, stash, reflog |
| 查看历史 | log, diff, show, blame, grep |
| 高级操作 | cherry-pick, bisect, submodule, worktree, filter-branch |
| 标签 | tag, push --tags |
| 归档补丁 | archive, format-patch, am, apply |
| 交互工具 | add -i, rebase -i, gui, gitk |
| 钩子 | pre-commit, post-receive 等 |
| 调试 | GIT_TRACE, cat-file, hash-object |
Git 几乎可以完成任何与版本控制相关的需求:从基本的文件版本管理,到复杂的多人协作工作流(GitFlow、GitHub Flow),再到历史改写、性能优化、仓库迁移、自动化集成等。掌握这些操作,你就能成为 Git 专家。