Git 命令速查
按基础、分支、远程、撤销、暂存、日志、标签、子模块、变基等分类速查 Git 常用命令,支持关键字搜索与一键复制。
浏览器本地运行所有计算都在你的浏览器里完成,数据不会离开本机。
共 187 / 187 条
| 命令 | 说明 |
|---|---|
git init | 在当前目录初始化新的 Git 仓库 Initialize a new repository in the current directory |
git clone <url> | 克隆远程仓库到本地 Clone a remote repository |
git clone --depth 1 <url> | 浅克隆:只取最近一次提交,适合大仓库 Shallow clone with only the latest commit, useful for huge repositories |
git clone --recurse-submodules <url> | 克隆仓库并同时检出子模块 Clone a repository including its submodules |
git status | 查看工作区与暂存区状态 Show the status of the working tree and the index |
git status -s | 用简洁的两列格式输出状态 Show the status in the compact two-column format |
git status -sb | 简洁格式并显示当前分支与上游差距 Compact status with branch and upstream tracking info |
git add <file> | 把指定文件的改动加入暂存区 Stage the changes of a file |
git add -A | 暂存全部改动(包含新增与删除) Stage every change, including new and deleted files |
git add -u | 只暂存已跟踪文件的修改与删除 Stage modifications and deletions of tracked files only |
git add -p | 交互式逐块挑选要暂存的内容 Interactively pick the hunks to stage |
git add -f <file> | 强制暂存被 .gitignore 忽略的文件 Force-add a file that .gitignore excludes |
git commit -m "msg" | 提交暂存区的改动 Commit the staged changes |
git commit -am "msg" | 暂存已跟踪文件的改动并提交 Stage tracked files and commit in one step |
git commit --amend | 修改最近一次提交(内容与提交信息) Amend the most recent commit, message included |
git commit --amend --no-edit | 把暂存内容并入最近一次提交但不改提交信息 Fold staged changes into the last commit, keeping its message |
git commit --allow-empty -m "msg" | 创建一次不含改动的空提交 Create an empty commit without changes |
git config --global user.name "name" | 设置全局用户名 Set the global user name |
git config --global user.email "mail" | 设置全局邮箱 Set the global email address |
git config --global core.editor vim | 设置默认的提交信息编辑器 Set the default commit message editor |
git config --global core.autocrlf input | 设置换行符处理策略(跨平台协作常用) Configure line ending handling, common for cross-platform work |
git config --global alias.st status | 配置命令别名,例如之后可用 git st Define a command alias, so git st works later |
git config --list --show-origin | 查看全部生效配置及其来源文件 List all effective configuration with its source file |
git help <command> | 查看命令手册 Open the manual for a command |
git rm <file> | 删除文件并把删除动作暂存 Delete a file and stage the deletion |
git rm --cached <file> | 停止跟踪文件但保留本地内容 Stop tracking a file while keeping it on disk |
git rm -r --cached <dir> | 递归停止跟踪目录(常用于补 .gitignore) Recursively untrack a directory, often after adding .gitignore |
git mv <old> <new> | 重命名文件并暂存这次改名 Rename a file and stage the rename |
git clean -n | 预演会删掉哪些未跟踪文件 Dry-run the untracked file cleanup |
git clean -fd | 删除未跟踪的文件与目录 Remove untracked files and directories |
git clean -fdx | 连同 .gitignore 忽略的文件一起删除 Also remove files that .gitignore excludes |
git check-ignore -v <file> | 查看文件命中了哪条忽略规则 Show which ignore rule matches a file |
git archive HEAD -o snapshot.zip | 把当前快照导出为压缩包(不含 .git) Export the current snapshot as an archive without the .git directory |
git branch | 列出本地分支 List local branches |
git branch -a | 列出本地与远程分支 List local and remote branches |
git branch -vv | 查看分支及其上游与领先、落后情况 Show branches with upstream and ahead/behind counts |
git branch <name> | 在当前提交上创建分支 Create a branch at the current commit |
git branch -m <old> <new> | 重命名分支 Rename a branch |
git branch -d <name> | 删除已经合并的分支 Delete a branch that is already merged |
git branch -D <name> | 强制删除分支(可能丢失提交) Force delete a branch even if it is unmerged |
git branch --merged | 列出已合并进当前分支的分支 List branches already merged into HEAD |
git branch --no-merged | 列出尚未合并的分支 List branches that are not merged yet |
git branch --contains <commit> | 查看哪些分支包含某个提交 Show which branches contain a commit |
git switch <branch> | 切换到分支 Switch to a branch |
git switch -c <branch> | 创建并切换到新分支 Create and switch to a new branch |
git switch -c <branch> <start-point> | 基于指定提交创建并切换分支 Create and switch to a branch starting from a given commit |
git switch - | 切回上一个分支 Switch back to the previous branch |
git switch --detach <commit> | 以分离头指针方式检出某个提交 Check out a commit in detached HEAD state |
git checkout <branch> | 切换分支(旧写法,等价 git switch) Switch branches, the older syntax of git switch |
git checkout -b <branch> | 创建并切换分支(旧写法) Create and switch to a branch, the older syntax |
git checkout -- <file> | 丢弃工作区中该文件的未暂存改动 Discard unstaged changes of a file |
git merge <branch> | 把指定分支合并进当前分支 Merge a branch into the current one |
git merge --no-ff <branch> | 禁止快进合并,保留一条合并提交 Merge without fast-forwarding, keeping a merge commit |
git merge --squash <branch> | 把分支所有改动压成一次待提交改动 Squash all changes of a branch into one staged change |
git merge --no-commit <branch> | 合并但不自动提交,便于检查结果 Merge without committing so you can inspect the result |
git merge --abort | 放弃正在进行的合并 Abort the merge in progress |
git merge-base <a> <b> | 找出两个分支的共同祖先提交 Find the common ancestor of two branches |
git remote -v | 查看远程仓库名称与地址 List remotes with their URLs |
git remote add origin <url> | 添加远程仓库 Add a remote repository |
git remote remove <name> | 删除远程仓库配置 Remove a remote configuration |
git remote set-url origin <url> | 修改远程仓库地址 Change a remote URL |
git remote rename <old> <new> | 重命名远程仓库 Rename a remote |
git remote show origin | 查看远程仓库详情与分支对应关系 Show details of a remote and its branch mapping |
git fetch | 拉取远程更新但不合并 Fetch remote updates without merging |
git fetch --all --prune | 拉取全部远程并清理已删除的远程分支 Fetch all remotes and prune deleted remote branches |
git fetch origin <branch> | 只拉取指定远程分支 Fetch a single remote branch |
git pull | 拉取并合并远程分支 Fetch and merge the remote branch |
git pull --rebase | 拉取后用变基方式整合本地提交 Fetch and rebase local commits on top |
git pull --ff-only | 只在能快进时拉取,避免意外合并 Only pull when a fast-forward is possible |
git push | 推送当前分支到它的上游 Push the current branch to its upstream |
git push -u origin main | 首次推送并设置上游分支 Push for the first time and set the upstream |
git push --force-with-lease | 安全强推:远程有别人的新提交时会失败 Force push safely: fails if someone else pushed first |
git push -f | 强制推送,会覆盖远程历史(谨慎使用) Force push, overwriting remote history — use with care |
git push origin --delete <branch> | 删除远程分支 Delete a remote branch |
git push <remote> <local>:<remote-branch> | 把本地分支推送到指定远程分支名 Push a local branch to a differently named remote branch |
git ls-remote --heads origin | 列出远程分支引用(不下载对象) List remote head references without downloading objects |
git restore <file> | 把文件恢复到暂存区版本,丢弃工作区改动 Restore a file from the index, discarding working tree changes |
git restore --staged <file> | 把文件移出暂存区但保留改动 Unstage a file while keeping its changes |
git restore --source HEAD~2 <file> | 把文件恢复到两次提交之前的内容 Restore a file from two commits ago |
git restore :/ | 丢弃整个工作区的未暂存改动 Discard unstaged changes across the whole working tree |
git reset --soft HEAD~1 | 撤销最近一次提交,改动保留在暂存区 Undo the last commit, keeping its changes staged |
git reset HEAD~1 | 撤销最近一次提交,改动回到工作区 Undo the last commit, keeping its changes unstaged |
git reset --hard HEAD~1 | 撤销提交并丢弃所有改动 Undo the last commit and throw the changes away |
git reset --hard origin/main | 把本地分支强制对齐到远程版本 Hard reset the local branch onto the remote version |
git reset -p | 交互式取消部分暂存内容 Interactively unstage hunks |
git revert <commit> | 新建一次提交来抵消指定提交 Create a new commit that undoes another one |
git revert --no-commit <commit> | 反向应用改动但不自动提交 Apply the inverse changes without committing |
git revert -m 1 <merge-commit> | 撤销一次合并提交(保留第一父提交方向) Revert a merge commit using its first parent |
git stash | 把未提交的改动暂存起来 Stash the uncommitted changes |
git stash push -m "msg" | 带说明地暂存改动 Stash the changes with a message |
git stash push -- <file> | 只暂存指定文件的改动 Stash changes of specific files only |
git stash list | 查看暂存列表 List the stashes |
git stash pop | 恢复最近一次暂存并删除该记录 Apply the latest stash and drop it |
git stash apply stash@{1} | 恢复指定暂存但保留记录 Apply a specific stash and keep it in the list |
git stash show -p stash@{0} | 查看某次暂存的具体改动 Show the diff stored in a stash |
git stash drop stash@{0} | 删除某条暂存记录 Drop a stash entry |
git stash clear | 清空全部暂存记录 Clear every stash |
git checkout <commit> -- <file> | 从指定提交里取出该文件的内容 Check out a file as of a specific commit |
git diff | 查看工作区与暂存区的差异 Show differences between the working tree and the index |
git diff --staged | 查看已暂存、即将提交的差异 Show the staged changes that are about to be committed |
git diff HEAD | 查看工作区相对最新提交的全部改动 Show all changes relative to HEAD |
git diff <a> <b> | 对比两个提交或分支的差异 Compare two commits or branches |
git diff --stat | 只显示改动统计 Show a diffstat instead of the full diff |
git diff --name-only | 只列出有改动的文件名 List only the changed file names |
git diff --word-diff | 按词显示差异,适合文档类文件 Show word-level differences, handy for prose |
git diff main...feature | 查看 feature 相对分叉点的改动 Show the changes on a branch since it diverged |
git add -i | 进入交互式暂存界面 Enter the interactive staging interface |
git update-index --assume-unchanged <file> | 让 Git 暂时忽略该文件的改动 Tell Git to ignore changes in a file for now |
git update-index --skip-worktree <file> | 标记文件为本地专用,抑制改动提示 Mark a file as locally modified and skip change tracking |
git apply <patch> | 把补丁文件应用到工作区 Apply a patch file to the working tree |
git format-patch -1 <commit> | 把一次提交导出为补丁文件 Export a commit as a patch file |
git am <patch> | 应用邮件格式补丁并保留提交信息 Apply a mailbox-format patch, keeping the commit message |
git add -N <file> | 只登记新文件而不暂存内容,便于查看 diff Register a new file without staging content so it shows in diffs |
git log | 查看提交历史 Show the commit history |
git log --oneline | 每条提交显示一行摘要 Show one summary line per commit |
git log --oneline --graph --decorate --all | 图形化查看全部分支的历史 Show a graph of every branch with ref decorations |
git log -p | 查看历史并附带每次提交的 diff Show history together with each patch |
git log --stat | 查看历史并附带改动统计 Show history with a per-commit diffstat |
git log -n 5 | 只看最近 5 条提交 Show only the last 5 commits |
git log --author="name" | 按作者筛选提交 Filter commits by author |
git log --since="2 weeks ago" | 按时间范围筛选提交 Filter commits by a date range |
git log --grep="fix" | 按提交信息关键字筛选 Search commit messages for a keyword |
git log -S "text" | 找出新增或删除过某段文本的提交 Find commits that added or removed a piece of text |
git log --follow <file> | 追踪文件被改名前的历史 Follow a file across renames |
git log <branch> --not main | 查看某分支独有的提交 Show commits reachable from a branch but not from main |
git log --merges --oneline | 只看合并提交 Show merge commits only |
git log --diff-filter=D --name-only | 列出被删除过的文件 List files that were deleted |
git show <commit> | 查看某次提交的完整内容 Show the full contents of a commit |
git show HEAD:<file> | 查看某次提交里该文件的内容 Show a file as of a commit |
git show --stat --oneline HEAD | 用一行摘要加统计查看最近一次提交 Show the last commit as a one-line summary with a diffstat |
git blame <file> | 逐行查看最后修改者与对应提交 Show the last author and commit for each line |
git blame -L 10,20 <file> | 只查看指定行范围的 blame Blame only a range of lines |
git shortlog -sn | 按作者统计提交数量 Summarize the commit count per author |
git reflog | 查看 HEAD 移动历史,可用来找回误删提交 Show the HEAD reflog, useful to recover lost commits |
git rev-parse HEAD | 输出 HEAD 的完整哈希值 Print the full hash of HEAD |
git tag | 列出所有标签 List all tags |
git tag -a v1.0.0 -m "release" | 创建带注释的标签 Create an annotated tag |
git tag v1.0.0 | 创建轻量标签 Create a lightweight tag |
git tag -a v1.0.1 <commit> | 给历史提交补打标签 Tag an older commit |
git tag -d v1.0.0 | 删除本地标签 Delete a local tag |
git push origin v1.0.0 | 推送单个标签到远程 Push a single tag to the remote |
git push --tags | 推送全部标签 Push all tags |
git push origin --delete v1.0.0 | 删除远程标签 Delete a remote tag |
git show v1.0.0 | 查看标签指向的提交 Show the commit a tag points to |
git describe --tags | 用最近的标签描述当前提交 Describe HEAD using the nearest tag |
git checkout tags/v1.0.0 | 检出某个标签对应的提交 Check out the commit a tag points to |
git tag --sort=-creatordate | 按创建时间倒序列出标签 List tags sorted by creation date, newest first |
git submodule add <url> <path> | 在指定路径添加子模块 Add a submodule at a path |
git submodule init | 初始化子模块配置 Initialize the submodule configuration |
git submodule update | 检出父仓库记录的子模块提交 Check out the submodule commits recorded by the parent |
git submodule update --init --recursive | 初始化并递归检出全部子模块 Initialize and check out all submodules recursively |
git submodule update --remote | 把子模块更新到其远程最新提交 Update submodules to the latest remote commit |
git submodule foreach "git pull" | 在每个子模块里执行同一条命令 Run the same command inside every submodule |
git submodule status | 查看子模块当前提交与状态 Show the recorded commit and status of each submodule |
git submodule deinit -f <path> | 取消子模块初始化(配合删除目录) Deinitialize a submodule before removing its directory |
git clone --recurse-submodules --remote <url> | 克隆并让子模块跟随远程分支 Clone and track the remote branch in submodules |
git rebase <branch> | 把当前分支的提交移到目标分支之上 Move the current branch on top of another branch |
git rebase -i HEAD~3 | 交互式整理最近 3 次提交 Interactively edit the last 3 commits |
git rebase --continue | 解决冲突后继续变基 Continue the rebase after resolving conflicts |
git rebase --abort | 放弃变基并回到初始状态 Abort the rebase and return to the original state |
git rebase --skip | 跳过当前补丁继续变基 Skip the current patch and continue |
git rebase --onto main base topic | 把 topic 上 base 之后的提交搬到 main 上 Replay the commits after base from topic onto main |
git rebase --autosquash -i HEAD~5 | 自动整理 fixup 与 squash 提交 Automatically arrange fixup and squash commits |
git pull --rebase origin main | 变基式拉取,避免多余的合并提交 Rebasing pull to avoid extra merge commits |
git cherry-pick <commit> | 把指定提交复制到当前分支 Copy a commit onto the current branch |
git cherry-pick <a>..<b> | 复制一段范围的提交 Cherry-pick a range of commits |
git cherry-pick -n <commit> | 只应用改动,不立即提交 Apply the changes without committing |
git cherry-pick --abort | 放弃正在进行的 cherry-pick Abort the cherry-pick in progress |
git bisect start | 开始二分查找定位引入问题的提交 Start a bisect session to find a bad commit |
git bisect good <commit> | 标记一个已知正常的提交 Mark a commit as known good |
git bisect bad | 把当前检出的提交标记为有问题 Mark the checked-out commit as bad |
git bisect reset | 结束二分查找并回到原分支 End the bisect session and go back |
git worktree add ../dir <branch> | 在另一个目录检出同一仓库的分支 Check out a branch of the same repository into another directory |
git worktree list | 列出全部工作树 List all worktrees |
git worktree remove ../dir | 删除工作树 Remove a worktree |
git grep "text" | 在已跟踪文件里搜索文本 Search tracked files for text |
git ls-files | 列出全部已跟踪文件 List every tracked file |
git notes add -m "note" | 给某次提交添加备注(不改提交哈希) Attach a note to a commit without changing its hash |
git cat-file -p <object> | 查看 Git 对象的原始内容 Show the raw content of a Git object |
git hash-object <file> | 计算文件内容的 Git 哈希 Compute the Git hash of a file content |
git symbolic-ref HEAD | 查看 HEAD 当前指向的分支 Show the branch HEAD points to |
git show-ref | 列出仓库内的全部引用 List every reference in the repository |
git gc | 清理并压缩仓库对象,回收空间 Garbage collect and compress repository objects |
git fsck --lost-found | 检查仓库完整性并找回悬空对象 Check repository integrity and recover dangling objects |
git count-objects -vH | 统计仓库对象占用空间 Show repository object size statistics |
git maintenance start | 注册后台仓库维护任务 Register background repository maintenance |
git bundle create repo.bundle --all | 把整个仓库打包成单个文件以便离线传递 Bundle the whole repository into a single file for offline transfer |
分类: 全部分类
这个工具能做什么
- 忘了 reset 和 revert 该用哪个:按「撤销」分类翻一遍,从只撤提交到彻底丢弃改动都有对应命令。
- 搜具体场景而不是背参数:输入「撤销」「stash 列表」「rebase」,命令本身和中英文说明会一起被匹配。
- 把命令贴进工单或文档:每条都能一键复制,不用再手敲容易写错的 --amend、--autosquash 之类的长参数。
- 给团队新人一份可检索的命令清单:共 187 条命令按 10 个分类整理,比翻 man 页快得多。
示例
输入
搜索「撤销」,分类「全部分类」
输出
git reset --soft HEAD~1 撤销最近一次提交,改动保留在暂存区 git reset HEAD~1 撤销最近一次提交,改动回到工作区 git reset --hard HEAD~1 撤销提交并丢弃所有改动 git revert -m 1 <merge-commit> 撤销一次合并提交(保留第一父提交方向)
搜索会同时匹配命令、中文说明和英文说明;多个关键字用空格分隔,必须全部命中(AND 关系)。
常见问题
搜索是模糊匹配还是精确匹配?
子串匹配,不区分大小写。多个关键字之间是 AND:输入「stash list」会返回同时包含 stash 和 list 的命令(也就是 git stash list 这类),只包含其中一个的不会出现。
reset --soft、reset、reset --hard 有什么区别?
三者都移动分支指针,区别在改动去哪:--soft 把改动留在暂存区,默认(--mixed)把改动退回工作区且取消暂存,--hard 直接丢弃所有改动。所以只有明确不要这些改动时才用 --hard。
revert 和 reset 该怎么选?
reset 改写历史,只适合还没推送的本地提交;revert 生成一次新提交来抵消指定提交,历史保留完整,已经推送或共享的分支应该用 revert。合并提交要撤销时用 revert -m 1 <merge-commit>。
这里的命令会在我电脑上执行吗?
不会。这只是一个速查表,页面只做筛选和复制,不会调用 git,也不会访问你的仓库。
命令数量以后会增加吗?
当前有 187 条,覆盖基础、分支、远程、撤销、暂存与差异、日志、标签、子模块、变基、其他共 10 个分类。分类数量会直接显示在筛选栏上,方便确认版本。
关键词:gitgit 命令cheat sheet速查rebasestashcherry-picksubmodulegit memo