参考 最近更新: 2026-11-23
Git 常用命令与工作流速查表
支持实时筛选的 Git CLI 速查表,覆盖仓库初始化、暂存、分支切换、变基、贮藏、历史排查、撤销与远程协作。
| Command | Action | Stage | Example |
|---|---|---|---|
| git init | Initialize a new empty Git repository in the current directory | Setup | git init -b main |
| git clone <url> | Download a remote repository and its commit history | Setup | git clone --depth 1 https://github.com/org/repo.git |
| git status -sb | Show concise working tree status and current branch | Inspect | git status -sb |
| git add -p | Interactively stage individual hunks before committing | Staging | git add -p src/ |
| git add -A | Stage all modified, new, and deleted files in the repo | Staging | git add -A |
| git restore --staged <file> | Unstage a file while preserving working directory edits | Staging | git restore --staged package.json |
| git restore <file> | Discard unstaged working tree modifications in a file | Undo | git restore src/index.ts |
| git commit -m "<msg>" | Record staged snapshot into repository history | Commit | git commit -m "feat: add schema generator" |
| git commit --amend --no-edit | Fold staged changes into the previous commit without changing its message | Commit | git commit --amend --no-edit |
| git diff | Show unstaged line-by-line differences in working directory | Inspect | git diff --stat |
| git diff --staged | Show changes currently staged for the next commit | Inspect | git diff --staged |
| git log --oneline --graph -n 15 | Display compact commit graph for recent 15 commits | Inspect | git log --oneline --graph --decorate -n 15 |
| git show <commit> | Inspect metadata and full diff for a specific commit or tag | Inspect | git show HEAD~1 --stat |
| git blame -L <start>,<end> <file> | Show who last modified each line in a range and in which commit | Inspect | git blame -L 10,40 src/auth.ts |
| git grep "<pattern>" | Fast search for patterns across tracked repository files | Inspect | git grep -n "parseCronExpression" |
| git switch -c <branch> | Create and switch to a new branch safely | Branching | git switch -c feat/curl-converter |
| git switch <branch> | Switch to an existing local or tracked remote branch | Branching | git switch main |
| git branch -vv | List local branches with upstream tracking status and last commit | Branching | git branch -vv |
| git branch -d <branch> | Delete a branch safely (only if already merged) | Branching | git branch -d feat/old-branch |
| git merge --no-ff <branch> | Merge branch while preserving an explicit merge commit | Merge | git merge --no-ff feat/api-v2 |
| git merge --squash <branch> | Combine all commits from a branch into staged changes for one commit | Merge | git merge --squash feat/cleanup |
| git rebase <base> | Reapply current branch commits sequentially on top of base branch | Rebase | git rebase origin/main |
| git rebase -i HEAD~<n> | Interactive rebase to squash, reword, reorder, or drop recent commits | Rebase | git rebase -i HEAD~4 |
| git cherry-pick <commit> | Apply changes from a specific commit onto the current branch | History | git cherry-pick a1b2c3d |
| git stash push -m "<msg>" | Temporarily shelve tracked dirty changes with a descriptive label | Stash | git stash push -m "wip: header parser" |
| git stash pop | Reapply the latest stash and remove it from the stash stack | Stash | git stash pop |
| git stash list | List all saved stash entries | Stash | git stash list |
| git fetch --prune | Fetch remote updates and prune deleted remote-tracking branches | Remote | git fetch --prune origin |
| git pull --rebase | Fetch remote commits and rebase local unpushed commits cleanly | Remote | git pull --rebase origin main |
| git push -u origin <branch> | Push local branch and set upstream tracking reference | Remote | git push -u origin feat/schema-guides |
| git push --force-with-lease | Safely force-push rewritten history only if remote matches expected tip | Remote | git push --force-with-lease origin feat/rebase |
| git reset --soft HEAD~1 | Undo the last commit while keeping its changes staged | Undo | git reset --soft HEAD~1 |
| git reset --hard <ref> | Reset HEAD, index, and working directory to target commit (destructive) | Undo | git reset --hard origin/main |
| git revert <commit> | Create a new inverse commit that safely undoes a published commit | Undo | git revert e4f5a6b |
| git reflog | View local HEAD movement history to recover lost or reset commits | Recovery | git reflog -n 20 |
| git bisect start | Binary search commit history to pinpoint when a bug was introduced | Debug | git bisect start HEAD v1.4.0 |
| git worktree add <path> <branch> | Check out a second branch simultaneously in a separate directory | Workspace | git worktree add ../hotfix main |
| git tag -a <tag> -m "<msg>" | Create an annotated release tag with tagger metadata and message | Release | git tag -a v2.1.0 -m "Release 2.1.0" |
未找到匹配的数据行
请尝试更换关键词,或清空筛选查看全部数据。
相关工具
相关教程
相关参考资料
常见问题
- When should I use git revert instead of git reset?
- Use git revert <commit> on shared or pushed branches because it records a new inverse commit without rewriting history. Reserve git reset for local unpushed commits where moving the branch pointer is safe.
- What is the difference between git switch/restore and git checkout?
- Modern Git separates git checkout into two safer, single-purpose commands: git switch creates or changes branches, while git restore unstages index entries or discards working-tree file modifications.
- How can I recover a commit lost after an accidental git reset?
- Run git reflog to list recent local HEAD movements, find the commit hash from before the reset, and recover it with git switch -c recovery <hash> or git reset --hard <hash>.