Git 速查表 - 常用命令快速参考

免费在线 Git 速查表,按分类整理的可搜索命令参考。快速查找分支、合并、变基、暂存、重置等 Git 命令。

Setup & Config

git init

Initialize a new Git repository in the current directory

--bare
git clone <url>

Clone a remote repository to your local machine

--depth 1--branch <name>--recurse-submodules
git config user.name "<name>"

Set the author name for commits

--global--local--list
git config user.email "<email>"

Set the author email for commits

--global--local

Staging & Commits

git add <file>

Stage a file for the next commit

-A-p.
git commit -m "<message>"

Create a commit with a message

--amend--no-edit-a--allow-empty
git status

Show the working tree status

-s--short
git diff

Show unstaged changes

--staged--cached--stat--name-only
git log

Show commit history

--oneline--graph--all-n <number>--author=<name>

Branching & Merging

git branch

List, create, or delete branches

-a-d <branch>-D <branch>-m <new-name>
git checkout <branch>

Switch to a branch or restore files

-b <new-branch>--
git switch <branch>

Switch to a branch (modern alternative to checkout)

-c <new-branch>--detach
git merge <branch>

Merge a branch into the current branch

--no-ff--squash--abort
git rebase <branch>

Reapply commits on top of another branch

-i--onto--abort--continue

Remote

git remote -v

List remote repositories with URLs

add <name> <url>remove <name>rename <old> <new>
git fetch

Download objects and refs from a remote

--all--prune<remote>
git pull

Fetch and integrate changes from a remote branch

--rebase--no-rebase<remote> <branch>
git push

Upload local commits to a remote repository

-u origin <branch>--force--tags--delete <branch>

Stash

git stash

Temporarily save uncommitted changes

-u--include-untracked-m "<message>"
git stash pop

Apply the most recent stash and remove it from the list

git stash list

List all stashed changes

git stash drop

Remove a specific stash entry

stash@{n}
git stash apply

Apply a stash without removing it from the list

stash@{n}

Undo & Reset

git reset <file>

Unstage a file while preserving changes

--soft HEAD~1--mixed HEAD~1--hard HEAD~1
git revert <commit>

Create a new commit that undoes a previous commit

--no-commit-n
git restore <file>

Discard changes in the working directory

--staged--source=<commit>
git clean -fd

Remove untracked files and directories

-n (dry run)-x-i

Inspection

git log --oneline

Show compact commit history (one line per commit)

git log --graph --all

Visualize branch history as a graph

git show <commit>

Display details and diff of a specific commit

git blame <file>

Show who last modified each line of a file

git reflog

Show a log of all reference updates (useful for recovery)

git shortlog -sn

Summarize commit counts by author

Tags

git tag <name>

Create a lightweight tag at the current commit

git tag -a <name> -m "<msg>"

Create an annotated tag with a message

git tag -l

List all tags

"v1.*"
git push origin <tag>

Push a specific tag to a remote

--tags
git tag -d <name>

Delete a local tag

什么是 Git 速查表?

Git 速查表是一份按分类整理的常用 Git 命令快速参考指南。Git 是全球最流行的分布式版本控制系统,被数百万开发者用于跟踪变更、协作开发和管理项目历史。面对数十个命令和数百个选项,即使是经验丰富的开发者也需要一份简洁的参考。本速查表涵盖了从基本设置和暂存到交互式变基、存储管理和历史检查等高级工作流程。

如何使用 Git 速查表

  1. 浏览按分类整理的命令:设置、暂存、分支、远程、储藏、撤销、检查和标签。
  2. 使用搜索框按关键词过滤命令——输入 'rebase'、'stash'、'reset' 或命令和描述的任何部分。
  3. 每个命令显示语法、简短描述和常用标志或选项。
  4. 点击任意命令旁边的复制按钮,将其复制到剪贴板以便在终端中使用。
  5. 使用每个命令下方列出的标志来发现你可能不知道的有用变体。

常见使用场景

  • 日常开发工作流 — 在日常开发工作流中快速查找提交、分支和推送代码的命令,无需离开浏览器。
  • 学习 Git — Git 新手?浏览有组织的分类来发现命令并了解每个命令的作用,包括常用标志和选项。
  • 解决合并冲突 — 在需要解决冲突或撤销错误时,找到正确的变基、合并、重置和还原命令。
  • 代码审查与检查 — 使用 git log、git blame 和 git show 等检查命令来审查提交历史、理解变更并追踪代码作者。

FAQ

git merge 和 git rebase 有什么区别?
两者都将一个分支的更改集成到另一个分支。'git merge' 创建一个合并提交,保留两个分支的完整历史。'git rebase' 将你的提交重新应用在目标分支之上,产生线性历史。Merge 对共享分支更安全;rebase 在合并前用于功能分支更干净。
如何撤销最后一次提交?
使用 'git reset --soft HEAD~1' 撤销提交但保留暂存的更改。使用 'git reset --mixed HEAD~1' 撤销提交并取消暂存。使用 'git reset --hard HEAD~1' 撤销提交并丢弃所有更改。如果提交已推送,请使用 'git revert' 创建安全的撤销提交。
git fetch 和 git pull 有什么区别?
'git fetch' 从远程仓库下载新数据但不将其集成到工作分支。'git pull' 本质上是 'git fetch' 加 'git merge'——它获取并立即集成更改。当你想在合并前审查更改时使用 fetch。

相关工具