你需要知道的一切都在这一张表中
Git 是一个版本控制系统,用于跟踪文件和目录的变更。它功能强大,适用于从小型个人项目到大型企业级应用的各种场景。
本指南是对最常用 Git 命令的快速参考,并非全面教程。
设置与配置
# 初始化一个新的 Git 仓库
git init
# 克隆远程仓库并在本地创建副本
git clone <url>
# 配置全局 Git 设置
git config --global <setting_name> <value>
# 为特定仓库配置本地 Git 设置
git config --local <setting_name> <value>
——————— 高级用法 ———————
# 显示 Git 配置摘要
git config --list
# 为 Git 提交消息设置自定义文本编辑器
git config --global core.editor "<editor_command>"
# 创建 Git 命令别名
git config --global alias.<shortcut> <command>
# 启用 Git 输出的自动着色
git config --global color.ui auto
# 缓存 Git 凭据一段时间
git config --global credential.helper 'cache --timeout=<seconds>'
# 配置 Git 检测特定类型的空白字符错误
git config --global core.whitespace <options>
# 在 fetch 时自动清理已删除的远程跟踪分支
git config --global fetch.prune true
# 设置自定义 diff 工具
git config --global diff.tool <tool>
# 设置自定义合并工具
git config --global merge.tool <tool>
# 使用自定义 diff 工具比较差异
git difftool
# 使用自定义合并工具解决冲突
git mergetool
文件操作
# 显示工作区状态
git status
# 将文件添加到暂存区
git add <file(s)>
# 从工作区和暂存区删除文件
git rm <file(s)>
# 移动或重命名文件
git mv <old_file> <new_file>
# 提交更改并附带提交信息
git commit -m "commit message"
# 显示工作区与上次提交之间的差异
git diff
——————— 高级用法 ———————
# 假设某个已跟踪文件未被修改(忽略其变更)
git update-index --assume-unchanged <file>
# 恢复对文件变更的正常跟踪
git update-index --no-assume-unchanged <file>
# 显示两个提交之间的差异
git diff <commit_id1>..<commit_id2>
# 取消暂存文件,但保留在工作目录中
git rm --cached <file_name>
分支与合并
# 列出所有本地分支
git branch
# 创建新分支
git branch <branch_name>
# 切换到指定分支
git checkout <branch_name>
# 将某分支合并到当前分支
git merge <branch_name>
# 删除指定分支
git branch -d <branch_name>
# 列出所有远程分支
git branch -r
——————— 高级用法 ———————
# 列出分支及其上游跟踪信息
git branch -vv
# 基于远程分支创建并切换到新本地分支
git checkout -b <branch_name> <remote_name>/<remote_branch>
# 在合并冲突时取消合并
git merge --abort
# 将当前分支变基(rebase)到另一个分支上
git rebase <branch_name>
# 取消正在进行的变基操作
git rebase --abort
# 交互式变基:可编辑、压缩、重排或丢弃提交
git rebase -i
# 交互式地将当前分支的提交变基到远程分支上
git rebase -i <remote_name>/<remote_branch>
远程仓库
# 列出所有远程仓库
git remote
# 添加远程仓库
git remote add <name> <url>
# 从远程仓库获取更新(不合并)
git fetch <remote_name>
# 从远程分支拉取并合并更改
git pull <remote_name> <remote_branch>
# 推送本地分支到远程仓库
git push <remote_name> <local_branch>
# 删除远程仓库配置
git remote rm <remote_name>
# 显示特定远程仓库的详细信息
git remote show <remote_name>
# 以详细模式显示远程仓库的跟踪分支
git remote show <remote_name> --verbose
——————— 高级用法 ———————
# 从所有远程仓库获取更新
git remote update
# 强制推送(覆盖远程历史,请谨慎使用)
git push --force <remote_name> <local_branch>
# 推送所有标签到远程仓库
git push --tags <remote_name>
# 重命名远程仓库
git remote rename <old_name> <new_name>
# 修改远程仓库的 URL
git remote set-url <name> <new_url>
# 清理已失效的远程跟踪分支
git remote prune <remote_name>
# 列出已合并到当前分支的所有远程分支
git branch -r --merged
# 列出尚未合并到当前分支的远程分支
git branch -r --no-merged
# 获取更新并自动清理过期的远程跟踪分支
git fetch -p
# 创建本地分支并跟踪远程分支
git branch --track <branch_name> <remote_name>/<remote_branch>
# 为现有本地分支设置远程跟踪
git branch -u <remote_name>/<remote_branch>
# 推送分支并设置上游跟踪
git push -u <remote_name> <local_branch>
# 解除本地分支与远程分支的跟踪关联
git branch --unset-upstream <branch_name>
提交历史
# 显示完整的提交历史
git log
# 显示简洁的单行提交历史
git log --oneline
# 显示带分支图的提交历史
git log --graph
# 按作者过滤提交历史
git log --author=<author_name>
# 显示从指定日期之后的提交
git log --since=<date>
# 显示到指定日期之前的提交
git log --until=<date>
标签(Tags)
# 列出所有标签
git tag
# 在指定提交上创建标签
git tag <tag_name> <commit_id>
# 创建带注释的标签(含说明信息)
git tag -a <tag_name> -m "tag message"
# 删除本地标签
git tag -d <tag_name>
# 删除远程标签
git push <remote_name> --delete <tag_name>
# 显示特定标签的详细信息
git show <tag_name>
贮藏(Stashes)
# 临时保存工作区的更改(可加说明)
git stash save "stash message"
# 列出所有贮藏项
git stash list
# 应用指定的贮藏(不删除)
git stash apply <stash>
# 删除指定的贮藏项
git stash drop <stash>
# 清空所有贮藏
git stash clear
挑拣提交(Cherry-Picking)
# 将某个提交从一个分支应用到当前分支
git cherry-pick <commit_id>
提交管理
# 修改最近一次提交(包括消息或内容)
git commit --amend
# 创建一个新提交来撤销某次提交的更改
git revert <commit_id>
# 丢弃所有更改并将 HEAD 重置到指定提交(危险!)
git reset --hard <commit_id>
# 将 HEAD 移动到指定提交,但保留暂存区内容
git reset --soft <commit_id>
# 显示本地 HEAD 的所有变更记录(用于找回丢失的提交)
git reflog
子模块、子树与高级子模块
# 向当前仓库添加子模块
git submodule add <repository_url> <path>
# 递归初始化并更新所有子模块
git submodule update --init --recursive
# 向当前仓库添加子树
git subtree add --prefix=<path> <repository_url>
# 初始化子模块(仅注册)
git submodule init
# 更新子模块到最新提交
git submodule update
# 在每个子模块中执行指定命令
git submodule foreach <command>
# 注销(反初始化)子模块
git submodule deinit <path>
钩子(Hooks)、自动化与 Diff/Merge 工具
# 查看钩子目录位置(通常在 .git/hooks/)
git hooks
# 常见钩子脚本名称(放入 hooks 目录即可生效)
pre-commit, post-commit, pre-push, post-merge 等
# 使钩子脚本可执行(确保能被触发)
chmod +x <hook_script>
补丁(Patches)操作
# 为指定提交生成补丁文件
git format-patch <commit_id>
# 应用补丁到当前分支
git apply <patch_file>
# 使用 "git am"(apply mailbox)方式应用补丁
git am <patch_file>
协作相关
# 生成 pull request 摘要(显示两个提交间的变更)
git request-pull <start_commit> <end_commit> <url>
# 汇总提交历史,列出作者及其贡献
git shortlog
# 列出 Git 跟踪的所有文件
git ls-files
# 在 Git 跟踪的文件中搜索指定模式
git grep <pattern>
二分查找、调试与性能优化
# 开始二分查找会话,定位引入 bug 的提交
git bisect start
# 标记某提交为“坏”(包含 bug)
git bisect bad <commit_id>
# 标记某提交为“好”(无 bug)
git bisect good <commit_id>
# 结束二分查找并返回原始状态
git bisect reset
# 验证 Git 仓库的完整性
git fsck
# 执行垃圾回收以优化仓库性能
git gc
# 删除未跟踪的文件和目录(谨慎使用!)
git clean -df
技巧与窍门
# 交互式选择文件中的部分变更(hunks)进行暂存
git add -p
# 显示特定文件的提交历史及对应补丁
git log -p <file_name>
# 自定义 git log 的输出格式
git log --pretty=format:"%h - %an, %ar : %s"
# 在提交信息中搜索关键词
git log --grep="<text>"
# 快速查看自上次提交以来的工作区变更统计
git diff --stat
# 显示带装饰和图形的分支历史(清晰展示分支分合)
git log --oneline --decorate --graph
# 贮藏时包含未跟踪的文件
git stash save -u
# 创建空提交(常用于测试分支保护规则)
git commit --allow-empty -m "Empty commit message"
# 设置 Git 分页器:内容少于一屏时不翻页,且不清屏
git config --global core.pager 'less -RFX'
# 启用 Git 自动纠正拼写错误的命令
git config --global help.autocorrect 1
# 列出所有 Git 命令别名
git config --get-regexp alias
# 模拟合并(不实际提交,用于预览)
git merge --no-commit --no-ff <branch_name>
# 以树状结构显示仓库内容
git ls-tree --name-only -r -t HEAD