wzedc.tech / git-cheat-sheet

Git 命令速查表

新手速查:9 个分类 45 条最常用命令,一条只写一个最简形式。点一下命令即可复制。

9 个分类 · 45 条命令

git 固定前缀 switch 子命令:做什么 --staged 选项:附加要求 <file> 参数:替换成你自己的内容

需要完整版?

完整的 116 条速查表收录了进阶参数、暂存现场管理、排查与维护等命令,适合已经上手之后查阅。

打开完整版 →

GIT BASICS 基础


命令中文解释英文解释
git init <directory> 在指定目录创建一个空的 Git 仓库;不带参数则在当前目录创建 Create an empty Git repo in the specified directory; run it with no arguments to initialize the current directory.
git clone <repo> 克隆远程仓库到本地,<repo> 是仓库地址 Clone the repo onto your machine; <repo> is the repository address.
git add <file> 把文件的改动放进暂存区,准备提交;git add . 表示整个目录 Stage the changes of <file> for the next commit; use git add . for everything in the directory.
git commit -m "<message>" 提交暂存区内容,提交说明写在引号里 Commit the staged snapshot, using "message" as the commit message.
git status 查看哪些改动已暂存、未暂存,以及未跟踪的文件 List which files are staged, unstaged and untracked.
git log --oneline 每条提交压成一行,快速浏览提交历史 Show the commit history one line per commit.

GIT DIFF 查看改动


命令中文解释英文解释
git diff 工作区与暂存区的差异 Show unstaged changes between your index and the working directory.
git diff HEAD 工作区与上次提交的差异 Show the difference between the working directory and the last commit.
git diff --staged 暂存区与上次提交的差异 Show staged changes against the last commit.

UNDOING CHANGES 撤销修改


命令中文解释英文解释
git restore <file> 丢弃工作区对文件的修改(不可恢复) Discard the working-directory changes to <file>.
git restore --staged <file> 把文件移出暂存区,改动仍留在工作区 Remove <file> from the staging area, keeping the changes in the working directory.
git revert <commit> 生成一个反向提交抵消指定提交,已推送的提交用这个撤销 Create a new commit that undoes the changes of <commit>; use it for commits you have already pushed.
git stash 把当前未提交的改动先收起来,工作区变干净 Stash your uncommitted changes away and leave the working directory clean.
git stash pop 恢复最近一次收起的改动 Restore the most recent stash.

REWRITING GIT HISTORY 改写历史


命令中文解释英文解释
git commit --amend --no-edit 把当前改动并进上一次提交,提交说明不变 Add the staged changes to the last commit and keep its message.
git rebase <base> 把当前分支的提交挪到 <base> 之上,整理历史 Rebase the current branch onto <base> to tidy up the history.
git rebase -i <base> 交互式整理提交:合并、重排、改写、删除 Interactively rebase onto <base>: squash, reorder, reword or drop commits.
git reflog 查看 HEAD 的移动记录,误操作后从这里找回 Show a log of changes to HEAD; this is where you undo mistakes.

GIT BRANCHES 分支


命令中文解释英文解释
git branch 列出本地分支 List the branches in your repo.
git switch <branch> 切换到已有分支 Switch to an existing branch.
git switch -c <branch> 创建并切换到新分支 Create and switch to a new branch named <branch>.
git switch - 回到上一个分支 Switch back to the previous branch.
git merge <branch> 把指定分支合并进当前分支 Merge <branch> into the current branch.

REMOTE REPOSITORIES 远程仓库


命令中文解释英文解释
git remote add <name> <url> 添加一个远程仓库,之后可用 <name> 指代它 Add a remote repo; afterwards <name> stands for <url>.
git fetch <remote> 抓取远程的提交,但不动你的本地分支 Fetch the remote's commits without touching your local branches.
git pull --rebase <remote> 抓取远程,并把自己的提交叠到远程之后(历史更干净) Fetch the remote and put your own commits on top of it, keeping the history linear.
git push <remote> <branch> 把本地分支推送到远程;远程没有该分支会自动创建 Push the branch to the remote; it is created there if missing.
git push -u origin <branch> 首次推送并建立跟踪关系,之后直接 git push 即可 Push a new branch and set its upstream, so a plain git push works afterwards.
git push --force-with-lease 强推,但只有远程仍是你见过的版本时才生效(比 --force 安全) Force-push only if the remote ref is still the one you have seen; safer than --force.
git push --follow-tags 推送提交时,一并推送相关的标签 Push the commits together with the tags that point at them.

GIT LOG 查看历史


命令中文解释英文解释
git log - <limit> 只看最近几条,例如 git log -5 Limit the number of commits shown, e.g. git log -5.
git log --author= "<pattern>" 按作者筛选提交 Search for commits by a particular author.
git log --grep= "<pattern>" 按提交说明的内容筛选提交 Search for commits whose message matches <pattern>.
git log <since> .. <until> 查看两个引用之间的提交 Show the commits between <since> and <until>.
git log -- <file> 只看涉及某个文件的提交 Show only the commits that touched <file>.
git log --graph 用文本图形展示提交与分支结构 Draw a text-based graph of the commits.

GIT RESET 回退


命令中文解释英文解释
git reset 清空暂存区,工作区保持不变 Reset the staging area to the last commit, leaving the working directory unchanged.
git reset <commit> 回退到指定提交,改动退回工作区(保留内容) Move the branch tip back to <commit> and keep the changes in the working directory.
git reset --hard 丢弃全部未提交的改动(危险) Discard every uncommitted change in the staging area and working directory.
git reset --hard <commit> 回退到指定提交,并丢弃其后的所有提交与改动(危险) Move the branch back to <commit> and delete everything after it.

GIT CONFIG 配置


命令中文解释英文解释
git config --global user.name "<name>" 设置提交者姓名(第一次用 Git 时配置一次) Set the author name used for all your commits.
git config --global user.email "<email>" 设置提交者邮箱 Set the author email used for all your commits.
git config --global core.editor "<editor>" 设置 Git 打开的默认编辑器,例如 vim、nvim Set the text editor Git launches, e.g. vim or nvim.
git config --global alias. <alias-name> "<git-command>" 配置命令别名,例如给 status 起个短名 Create a shortcut for a Git command, e.g. a short name for status.
git config --global --edit 打开全局配置文件手动编辑 Open the global configuration file in your editor.