首页Git速查表

Git速查表

在线Git速查表,汇总配置、初始化、提交、回退与分支等常用命令,支持分类速查和一键复制

命令分类

基础操作
远程协作
仓库维护
高级与修复

当前显示命令

122

高风险命令

29

命令速查

配置

3

设置全局身份信息

git config --global user.name "[name]"
git config --global user.email "[email]"

查看全部配置

git config --list

查看指定配置项

git config user.name

开始使用

3

创建 Git 仓库

git init

克隆已有仓库

git clone [url]

克隆指定分支

git clone -b [branch-name] [url]

状态与检查

4

查看工作区状态

git status

查看未暂存改动

git diff

查看已暂存改动

git diff --staged

查看最近一次提交详情

git show HEAD

暂存区

4

暂存所有改动

git add .

暂存指定文件

git add [file]

取消暂存指定文件

git restore --staged [file]

丢弃工作区指定文件改动

高风险
git restore [file]

提交

4

提交暂存区内容

git commit -m "[commit message]"

提交全部已跟踪改动

git commit -am "[commit message]"

修改最近一次提交并保留原说明

高风险
git commit --amend --no-edit

修改最近一次提交说明

高风险
git commit --amend -m "[commit message]"

分支

8

查看本地分支

git branch

查看本地与远程分支

git branch -a

创建新分支

git branch [branch-name]

切换到指定分支

git switch [branch-name]

创建并切换分支

git switch -c [branch-name]

删除已合并分支

git branch -d [branch-name]

强制删除分支

高风险
git branch -D [branch-name]

重命名当前分支

git branch -m [new-branch-name]

远程仓库

4

查看远程仓库

git remote -v

添加远程仓库

git remote add origin [url]

修改远程仓库地址

git remote set-url origin [url]

删除远程仓库

高风险
git remote remove origin

同步

6

获取远程更新

git fetch origin

拉取并合并指定分支

git pull origin [branch-name]

拉取并使用 rebase

git pull --rebase origin [branch-name]

推送到指定远程分支

git push origin [branch-name]

推送并设置上游分支

git push -u origin [branch-name]

安全地强制推送

高风险
git push --force-with-lease origin [branch-name]

历史与对比

4

查看精简提交历史

git log --oneline --graph --decorate --all

查看文件提交历史

git log -- [file]

查看文件每行最后修改者

git blame [file]

查看引用变动记录

git reflog

打包仓库

4

创建完整仓库 bundle 文件

git bundle create repo.bundle --all

校验 bundle 文件内容

git bundle verify repo.bundle

从 bundle 克隆仓库

git clone repo.bundle [dir-name]

从 bundle 拉取更新

git pull repo.bundle [branch-name]

归档导出

3

导出当前分支 ZIP 归档

git archive --format=zip --output=project.zip HEAD

导出指定标签归档

git archive --format=tar.gz --output=release.tar.gz [tag-name]

导出指定子目录

git archive --format=zip --output=folder.zip HEAD:[path]

提交注释

5

查看提交注释列表

git notes list

为当前提交添加注释

git notes add -m "[note]"

查看指定提交注释

git notes show [commit-hash]

追加提交注释内容

git notes append -m "[note]" [commit-hash]

删除指定提交注释

高风险
git notes remove [commit-hash]

历史清理

4

分析仓库历史结构

git filter-repo --analyze

删除指定路径历史

高风险
git filter-repo --path [path] --invert-paths --force

只保留指定路径历史

高风险
git filter-repo --path [path] --force

批量替换历史中的敏感文本

高风险
git filter-repo --replace-text replacements.txt --force

二分排查

4

开始二分排查

git bisect start

标记当前提交为有问题

git bisect bad

标记当前提交为正常

git bisect good [commit-hash]

重置并结束二分排查

git bisect reset

仓库检查

4

检查仓库对象完整性

git fsck

显示不可达对象

git fsck --unreachable

检查并显示完整对象列表

git fsck --full

清理并整理仓库对象

高风险
git gc --prune=now

仓库维护

4

启用后台维护任务

git maintenance start

执行自动维护

git maintenance run --auto

注册当前仓库到维护计划

git maintenance register

停止后台维护任务

git maintenance stop

变基

4

将当前分支变基到目标分支

高风险
git rebase [branch-name]

继续处理中的变基

高风险
git rebase --continue

跳过当前冲突提交

高风险
git rebase --skip

中止当前变基

git rebase --abort

对象替换

4

用新对象替换旧对象引用

高风险
git replace [old-object] [new-object]

查看全部替换规则

git replace -l

编辑替换对象

高风险
git replace --edit [object-id]

删除替换规则

高风险
git replace -d [object-id]

冲突复用

5

启用 rerere 自动记录冲突解决

git config rerere.enabled true

查看当前 rerere 状态

git rerere status

查看 rerere 记录的冲突差异

git rerere diff

忘记指定文件的冲突解决记录

高风险
git rerere forget [path]

清空全部 rerere 记录

高风险
git rerere clear

凭据管理

5

配置 Git Credential Manager

git config --global credential.helper manager-core

配置内存凭据缓存

git config --global credential.helper cache

写入凭据助手记录

git credential approve

读取匹配的凭据

git credential fill

删除匹配的凭据

高风险
git credential reject

稀疏检出

4

初始化稀疏检出

git sparse-checkout init --cone

设置保留目录

git sparse-checkout set [path1] [path2]

追加保留目录

git sparse-checkout add [path]

关闭稀疏检出

git sparse-checkout disable

暂存栈

5

保存当前改动到暂存栈

git stash push -m "[message]"

查看暂存栈列表

git stash list

恢复并删除最新暂存

git stash pop

恢复指定暂存但保留记录

git stash apply stash@{0}

删除指定暂存记录

高风险
git stash drop stash@{0}

标签

5

查看标签列表

git tag

创建轻量标签

git tag [tag-name]

创建附注标签

git tag -a [tag-name] -m "[tag message]"

推送指定标签

git push origin [tag-name]

推送全部标签

git push origin --tags

钩子

4

指定自定义 hooks 目录

git config core.hooksPath .githooks

查看当前 hooks 目录配置

git config core.hooksPath

取消自定义 hooks 目录

高风险
git config --unset core.hooksPath

让钩子脚本具备执行权限

chmod +x .githooks/pre-commit

子模块

4

添加子模块

git submodule add [url] [path]

初始化并拉取全部子模块

git submodule update --init --recursive

更新子模块到远程最新提交

git submodule update --remote --recursive

查看子模块状态

git submodule status

工作树

4

查看工作树列表

git worktree list

为新分支创建工作树

git worktree add ../[dir-name] -b [branch-name]

移除工作树

高风险
git worktree remove ../[dir-name]

清理失效工作树记录

git worktree prune

回退与修复

6

撤销最近一次提交并保留改动

高风险
git reset HEAD~1

软回退最近一次提交

高风险
git reset --soft HEAD~1

撤销最近 N 次提交并保留改动

高风险
git reset HEAD~N

撤销最近一次提交并丢弃改动

高风险
git reset HEAD~1 --hard

回滚指定提交并生成新提交

git revert [commit-hash]

将当前分支重置到远程状态

高风险
git fetch origin
git reset --hard origin/[branch-name]

杂项

4

将本地 master 重命名为 main

git branch -m master main

挑拣指定提交到当前分支

git cherry-pick [commit-hash]

合并指定分支到当前分支

git merge [branch-name]

删除未跟踪文件和目录

高风险
git clean -fd


说明文档

关于 Git 速查表

该工具整理了 Git 配置、状态检查、暂存、提交、分支、远程、同步、历史、仓库打包、归档导出、提交注释、历史清理、二分排查、仓库检查、仓库维护、变基、对象替换、冲突复用、凭据管理、稀疏检出、标签、钩子、子模块、工作树、暂存栈和回退修复等常用命令,适合在开发过程中快速查找并复制执行。

核心功能

  • 分类速查:按配置、状态检查、暂存、提交、分支、远程、同步、历史、仓库打包、归档导出、提交注释、历史清理、二分排查、仓库检查、仓库维护、变基、对象替换、冲突复用、凭据管理、稀疏检出、标签、钩子、子模块、工作树和回退修复等分类展示命令。
  • 一键复制:支持复制单条命令,也可复制当前筛选结果中的全部命令。
  • 关键词筛选:可按命令名称和命令文本快速定位目标操作。
  • 风险标记:对 resetamend--hard 等高风险命令进行醒目标记。

使用说明

  1. 先通过顶部标签切换到目标命令分类。
  2. 如命令较多,可输入关键词进一步筛选。
  3. 确认命令作用后点击复制按钮,再粘贴到终端执行。
  4. 执行历史改写或强制回退前,先核对当前仓库状态。

使用场景

  • Git 新手记忆常用命令时快速查阅。
  • 团队协作前核对提交、推送、分支和远程仓库处理命令。
  • 处理误提交、误修改、标签发布或远程同步异常时临时检索命令。

常见问题

这个工具会直接执行 Git 命令吗?

不会。页面只负责展示和复制命令,不会访问本地仓库或自动执行终端操作。

为什么有些命令被标记为高风险?

因为 amendreset--hard 可能改写历史或丢弃改动,执行前应确认影响范围。