这份Git命令速查表收集了最常用的Git命令,按功能分类整理,方便快速查找和使用。每个命令都附带简短说明和常见用例。
使用上方搜索框可以快速查找特定命令,或使用分类筛选器按类别浏览。点击命令卡片可以查看详细说明。
设置与配置
这些命令用于Git的初始安装和配置。
初级
git config
配置Git参数
常用选项
# 设置用户名和邮箱
git config --global user.name "你的名字"
git config --global user.email "your.email@example.com"
# 设置默认编辑器
git config --global core.editor "code --wait"
# 设置默认分支名
git config --global init.defaultBranch main
# 查看配置
git config --list
初级
git init
初始化Git仓库
常用选项
# 在当前目录初始化仓库
git init
# 在指定目录初始化仓库
git init <directory>
# 创建裸仓库(没有工作目录)
git init --bare
初级
git clone
克隆远程仓库
常用选项
# 克隆仓库
git clone <repository-url>
# 克隆仓库到指定目录
git clone <repository-url> <directory>
# 克隆特定分支
git clone -b <branch> <repository-url>
# 浅克隆(只获取最近的提交)
git clone --depth=1 <repository-url>
初级
git help
获取帮助信息
常用选项
# 获取特定命令的帮助
git help <command>
# 查看简要帮助
git <command> -h
基本命令
这些是日常使用Git时最常用的基本命令。
初级
git add
添加文件到暂存区
常用选项
# 添加单个文件
git add <file>
# 添加多个文件
git add <file1> <file2>
# 添加所有更改
git add .
# 添加所有更改(包括删除的文件)
git add -A
# 交互式添加
git add -i
初级
git commit
提交更改到仓库
常用选项
# 提交已暂存的更改
git commit -m "提交信息"
# 跳过暂存区直接提交
git commit -a -m "提交信息"
# 修改上一次提交
git commit --amend
# 空提交(仅更新钩子)
git commit --allow-empty -m "空提交"
初级
git status
查看工作目录状态
常用选项
# 查看详细状态
git status
# 查看简洁状态
git status -s
# 包含忽略的文件
git status --ignored
git diff
显示更改差异
常用选项
# 查看未暂存的更改
git diff
# 查看已暂存的更改
git diff --staged
# 查看两个提交之间的差异
git diff <commit1> <commit2>
# 查看特定文件的差异
git diff -- <file>
git log
查看提交历史
常用选项
# 查看完整历史
git log
# 查看简洁历史
git log --oneline
# 查看图形化历史
git log --graph --oneline
# 查看特定文件的历史
git log -- <file>
# 查看最近n次提交
git log -n <number>
git reset
重置当前HEAD
常用选项
# 重置暂存区
git reset HEAD <file>
# 软重置(保留工作目录和暂存区)
git reset --soft <commit>
# 混合重置(保留工作目录,重置暂存区)
git reset --mixed <commit>
# 硬重置(丢弃所有更改)
git reset --hard <commit>
分支与合并
这些命令用于创建和管理分支,以及合并更改。
中级
git branch
分支管理
常用选项
# 查看本地分支
git branch
# 查看所有分支(包括远程)
git branch -a
# 创建新分支
git branch <branch-name>
# 删除分支
git branch -d <branch-name>
# 强制删除分支
git branch -D <branch-name>
中级
git checkout
切换分支或恢复文件
常用选项
# 切换分支
git checkout <branch-name>
# 创建并切换分支
git checkout -b <new-branch>
# 恢复文件
git checkout -- <file>
# 切换到特定提交
git checkout <commit>
初级
git switch
切换分支(Git 2.23+)
常用选项
# 切换分支
git switch <branch-name>
# 创建并切换分支
git switch -c <new-branch>
# 回到之前的分支
git switch -
中级
git merge
合并分支
常用选项
# 合并指定分支到当前分支
git merge <branch>
# 合并并创建合并提交
git merge --no-ff <branch>
# 合并但不自动提交
git merge --no-commit <branch>
# 中止合并
git merge --abort
高级
git rebase
变基操作
常用选项
# 变基到指定分支
git rebase <branch>
# 交互式变基
git rebase -i <commit>
# 继续变基
git rebase --continue
# 中止变基
git rebase --abort
高级
git cherry-pick
选择性合并提交
常用选项
# 挑选单个提交
git cherry-pick <commit>
# 挑选多个提交
git cherry-pick <commit1> <commit2>
# 不自动提交
git cherry-pick --no-commit <commit>
远程操作
这些命令用于与远程仓库交互。
初级
git remote
管理远程仓库
常用选项
# 查看远程仓库
git remote -v
# 添加远程仓库
git remote add <name> <url>
# 删除远程仓库
git remote remove <name>
# 重命名远程仓库
git remote rename <old> <new>
初级
git fetch
下载远程内容
常用选项
# 获取所有远程更改
git fetch
# 获取特定远程仓库的更改
git fetch <remote>
# 获取特定分支
git fetch <remote> <branch>
# 更新并清理已删除的远程分支
git fetch --prune
中级
git pull
获取并合并远程更改
常用选项
# 拉取并合并当前分支
git pull
# 从特定远程拉取特定分支
git pull <remote> <branch>
# 使用变基而非合并
git pull --rebase
# 拉取所有分支
git pull --all
中级
git push
推送本地更改到远程
常用选项
# 推送当前分支
git push
# 推送到特定远程和分支
git push <remote> <branch>
# 强制推送(谨慎使用)
git push --force
# 推送所有分支
git push --all
# 推送标签
git push --tags
高级命令
这些是一些更高级的Git命令,用于特定场景和任务。
中级
git stash
暂存工作目录的更改
常用选项
# 保存当前更改
git stash
# 保存并添加说明
git stash save "说明文字"
# 查看stash列表
git stash list
# 应用最近的stash
git stash apply
# 应用并删除最近的stash
git stash pop
# 应用特定的stash
git stash apply stash@{n}
# 删除所有stash
git stash clear
初级
git tag
标签管理
常用选项
# 列出所有标签
git tag
# 创建轻量级标签
git tag <tag-name>
# 创建带注释的标签
git tag -a <tag-name> -m "说明"
# 为特定提交创建标签
git tag -a <tag-name> <commit>
# 删除标签
git tag -d <tag-name>
# 推送标签到远程
git push origin <tag-name>
高级
git bisect
二分查找错误提交
常用选项
# 开始二分查找
git bisect start
# 标记当前版本为有问题
git bisect bad
# 标记已知好的版本
git bisect good <commit>
# 标记当前检出版本为好的
git bisect good
# 标记当前检出版本为坏的
git bisect bad
# 结束二分查找
git bisect reset
高级
git worktree
多工作目录管理
常用选项
# 添加新工作目录
git worktree add <path> <branch>
# 列出工作目录
git worktree list
# 删除工作目录
git worktree remove <path>
# 清理已删除的工作目录
git worktree prune
高级
git reflog
引用日志管理
常用选项
# 查看引用日志
git reflog
# 查看特定引用的日志
git reflog show <ref>
# 限制显示条数
git reflog -n <number>
中级
git clean
删除未跟踪文件
常用选项
# 查看将被删除的文件
git clean -n
# 删除未跟踪文件
git clean -f
# 删除未跟踪文件和目录
git clean -fd
# 删除包括忽略文件
git clean -fx
高级
git submodule
子模块管理
常用选项
# 添加子模块
git submodule add <repository> <path>
# 初始化子模块
git submodule init
# 更新子模块
git submodule update
# 拉取所有子模块
git submodule update --recursive --remote