Git コマンド早見表
基本・ブランチ・リモート・取り消し・ステージング・ログ・タグ・サブモジュール・リベースなどのカテゴリ別に Git のよく使うコマンドを引けます。キーワード検索とワンクリックのコピーに対応。
ブラウザ内で動作計算はすべてブラウザ内で行われ、データが端末の外に出ることはありません。
合計 187 / 187 件
| コマンド | 説明 |
|---|---|
git init | Initialize a new repository in the current directory 在当前目录初始化新的 Git 仓库 |
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> | Force-add a file that .gitignore excludes 强制暂存被 .gitignore 忽略的文件 |
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 | Define a command alias, so git st works later 配置命令别名,例如之后可用 git st |
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> | Recursively untrack a directory, often after adding .gitignore 递归停止跟踪目录(常用于补 .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 | Also remove files that .gitignore excludes 连同 .gitignore 忽略的文件一起删除 |
git check-ignore -v <file> | Show which ignore rule matches a file 查看文件命中了哪条忽略规则 |
git archive HEAD -o snapshot.zip | Export the current snapshot as an archive without the .git directory 把当前快照导出为压缩包(不含 .git) |
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> | Switch branches, the older syntax of git switch 切换分支(旧写法,等价 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 | Show the changes on a branch since it diverged 查看 feature 相对分叉点的改动 |
git add -i | Enter the interactive staging interface 进入交互式暂存界面 |
git update-index --assume-unchanged <file> | Tell Git to ignore changes in a file for now 让 Git 暂时忽略该文件的改动 |
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> | Register a new file without staging content so it shows in diffs 只登记新文件而不暂存内容,便于查看 diff |
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 | Show history together with each patch 查看历史并附带每次提交的 diff |
git log --stat | Show history with a per-commit diffstat 查看历史并附带改动统计 |
git log -n 5 | Show only the last 5 commits 只看最近 5 条提交 |
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 only a range of lines 只查看指定行范围的 blame |
git shortlog -sn | Summarize the commit count per author 按作者统计提交数量 |
git reflog | Show the HEAD reflog, useful to recover lost commits 查看 HEAD 移动历史,可用来找回误删提交 |
git rev-parse HEAD | Print the full hash of HEAD 输出 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 | Interactively edit the last 3 commits 交互式整理最近 3 次提交 |
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 | Replay the commits after base from topic onto main 把 topic 上 base 之后的提交搬到 main 上 |
git rebase --autosquash -i HEAD~5 | Automatically arrange fixup and squash commits 自动整理 fixup 与 squash 提交 |
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 | Abort the cherry-pick in progress 放弃正在进行的 cherry-pick |
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> | Show the raw content of a Git object 查看 Git 对象的原始内容 |
git hash-object <file> | Compute the Git hash of a file content 计算文件内容的 Git 哈希 |
git symbolic-ref HEAD | Show the branch HEAD points to 查看 HEAD 当前指向的分支 |
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 -m 1 <merge-commit> を使います。
ここにあるコマンドは自分のパソコンで実行されますか?
いいえ。これはチートシートで、ページは絞り込みとコピーだけを行います。git を呼び出すことも、あなたのリポジトリにアクセスすることもありません。
コマンドの数は今後増えますか?
現在 187 件で、基礎・ブランチ・リモート・取り消し・ステージと差分・ログ・タグ・サブモジュール・リベース・その他の 10 カテゴリをカバーしています。件数はフィルターバーにそのまま表示されるので、バージョンの確認に使えます。
キーワード:gitcheat sheetrebasestashcherry-picksubmodulegit memo