CodeToolProCodeToolProFree Online Developer Tools
GitHub

Git Command Reference

Init / Clone

git init

Initialize a new Git repository in the current directory

git init <name>

Create a new directory and initialize a Git repository

git clone <url>

Clone a remote repository to your local machine

git clone <url> <dir>

Clone into a specific directory name

git clone --depth 1 <url>

Shallow clone (only latest commit) for faster cloning

Branch

git branch

List all local branches (* marks current)

git branch -a

List all branches including remote tracking branches

git branch <name>

Create a new branch from the current HEAD

git branch -d <name>

Delete a local branch (safe, only if merged)

git branch -D <name>

Force delete a local branch (even if not merged)

git checkout <branch>

Switch to an existing branch

git checkout -b <name>

Create and switch to a new branch in one step

git switch <branch>

Switch to an existing branch (modern alternative to checkout)

git switch -c <name>

Create and switch to new branch (modern)

git merge <branch>

Merge specified branch into the current branch

git branch -m <old> <new>

Rename a branch

Commit

git status

Show working tree status: modified, staged, and untracked files

git add <file>

Stage a specific file for commit

git add .

Stage all changes in the current directory and subdirectories

git add -A

Stage all changes including deletions across entire repo

git commit -m 'message'

Commit staged changes with a message

git commit -am 'message'

Stage tracked files and commit in one step

git commit --amend

Amend the last commit (add changes / edit message)

git diff

Show unstaged changes between working tree and index

git diff --staged

Show staged changes that will go into the next commit

git reset <file>

Unstage a file while keeping changes in working tree

git reset --soft HEAD~1

Undo last commit but keep changes staged

git reset --hard HEAD~1

Undo last commit and discard all changes permanently

Merge / Rebase

git merge <branch>

Merge specified branch into the current branch

git merge --squash <branch>

Squash all commits from branch into a single commit

git merge --abort

Abort a merge in progress that has conflicts

git rebase <branch>

Reapply current branch commits on top of another branch

git rebase -i HEAD~<n>

Interactive rebase for the last n commits (squash, reword, etc.)

git rebase --continue

Continue a rebase after resolving conflicts

git rebase --abort

Abort a rebase and return to pre-rebase state

git cherry-pick <commit>

Apply a specific commit from another branch to current branch

Remote

git remote -v

List all remote repositories with their URLs

git remote add <name> <url>

Add a new remote repository

git remote remove <name>

Remove a remote repository

git push <remote> <branch>

Push local branch commits to remote repository

git push -u <remote> <branch>

Push and set upstream tracking for the first time

git push --force-with-lease

Force push with safety check (preferred over --force)

git fetch <remote>

Download objects and refs from remote without merging

git fetch --all

Fetch from all remotes

git pull <remote> <branch>

Fetch and merge remote changes into current branch

git pull --rebase

Fetch and rebase current branch on top of remote changes

Stash

git stash

Temporarily save all modified tracked files

git stash push -m 'message'

Stash changes with a descriptive message

git stash list

List all stashed changes

git stash pop

Apply the most recent stash and remove it from stash list

git stash apply

Apply the most recent stash without removing it

git stash apply stash@{<n>}

Apply a specific stash by index

git stash drop stash@{<n>}

Delete a specific stash

git stash clear

Remove all stashes

Log / Diff

git log

Show commit history in reverse chronological order

git log --oneline

Show compact commit history (one line per commit)

git log --graph --oneline --all

Show visual branch graph of all commits

git log -p

Show commit history with full diff for each commit

git log --author='name'

Show commits by a specific author

git log --since='2 weeks ago'

Show commits from the last two weeks

git show <commit>

Show details of a specific commit including diff

git diff <branch1>..<branch2>

Show differences between two branches

git diff <file>

Show changes to a specific file

git blame <file>

Show who last modified each line of a file

git reflog

Show reference log of all HEAD movements (recovery tool)

Undo

git checkout -- <file>

Discard changes to a file in the working tree

git restore <file>

Restore a file from the index or HEAD (modern)

git restore --staged <file>

Unstage a file (modern alternative to git reset <file>)

git reset --soft HEAD~<n>

Undo last n commits but keep changes staged

git revert <commit>

Create a new commit that undoes a specific commit (safe)

git clean -fd

Remove all untracked files and directories

git commit --amend -m 'new msg'

Change the last commit message

Showing 72 of 72 commands

技术详情

Git 命令速查表

工具功能

Git 命令速查表提供了按分类整理的 Git 常用命令合集,每个命令包含简介、语法格式和常见参数说明。分类覆盖:仓库初始化(init、clone)、日常开发(add、commit、status、diff)、分支管理(branch、checkout、switch、merge、rebase)、远程协作(remote、fetch、pull、push)、历史查看(log、blame、show、reflog)、撤销操作(reset、revert、restore、stash)和高级操作(cherry-pick、bisect、tag、submodule)。支持关键词搜索快速定位命令。


常见开发者使用场景

Git 命令速查表是日常开发中的必备参考。新入职的开发者面对陌生的 Git 工作流(Git Flow、GitHub Flow、Trunk-based Development)时,速查表帮助快速上手。在解决 Git 冲突或需要撤销错误操作的紧急情况下,clone-of-direction 的命令是救命稻草。编写 CI/CD 配置文件(GitHub Actions、GitLab CI)需要精确的 Git 命令语法。高级操作如交互式 rebase(git rebase -i)的参数和流程也可以在速查表中找到清晰指引。

配合 HTTP 状态码参考 查询 Git 远程操作中的网络相关状态,或使用 文本差异对比器 可视化 Git 提交之间的代码差异。


Git 核心概念与对象模型

理解 Git 的底层模型帮助掌握这些命令:

  • 三棵树模型:工作目录(Working Directory)→ 暂存区(Staging Area / Index)→ 仓库(Repository / HEAD)。git add 从工作目录到暂存区,git commit 从暂存区到仓库。
  • SHA-1 标识:每个 commit、tree、blob 都由一个 40 字符的 SHA-1 哈希值唯一标识。Git 正逐步迁移到 SHA-256 以解决潜在碰撞问题。
  • 分支指针:Git 分支本质上是一个指向特定 commit 的可移动指针,创建分支仅仅是创建一个 41 字节的文件。
  • Detached HEAD:当 HEAD 直接指向某个 commit 而非分支时,处于分离头状态——在此状态下创建的 commit 没有分支引用,容易丢失。

常见陷阱与注意事项

  • git reset --hard 的破坏性:此命令会永久删除未提交的更改。在执行前先确认 git status,并考虑先使用 git stash 备份。
  • git push --force:强制推送会覆盖远程历史,对协作分支使用极度危险。推荐使用 git push --force-with-lease,它会在远程有他人提交时拒绝推送。
  • merge vs rebase:公共分支上永远不要进行 rebase——这会改写提交历史,导致团队成员的工作冲突。rebase 仅用于本地私人分支。
  • 大文件:不要将二进制文件、构建产物、node_modules 提交到 Git 仓库。使用 .gitignore 排除,或使用 Git LFS 管理大文件。

何时使用此工具而非代码

在日常开发中作为命令行记忆辅助、快速查找不常用的 Git 命令参数、或在团队中统一 Git 操作规范时参考使用。