Git 常用命令速查手册

Git 常用命令速查手册

一份精心整理的 Git 命令参考文档,涵盖日常开发中最常用的操作。


目录

  1. 配置与初始化
  2. 基础操作
  3. 分支管理
  4. 远程操作
  5. 撤销与恢复
  6. 储藏与清理
  7. 标签管理
  8. 日志与查看
  9. 补丁操作
  10. 高级技巧

一、配置与初始化

全局配置

# 设置用户名和邮箱(全局)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 设置默认编辑器
git config --global core.editor "vim"
# 或 VS Code
git config --global core.editor "code --wait"

# 设置默认分支名为 main
git config --global init.defaultBranch main

# 查看所有配置
git config --list

# 查看某项配置
git config user.name

仓库初始化

# 初始化新仓库
git init

# 克隆远程仓库
git clone <repository-url>

# 克隆指定分支
git clone -b <branch-name> <repository-url>

# 克隆到指定目录
git clone <repository-url> <directory-name>

二、基础操作

查看状态

# 查看工作区状态
git status

# 简洁状态显示
git status -s
# 或
git status --short

添加文件到暂存区

# 添加指定文件
git add <file-name>

# 添加所有修改
git add .

# 添加所有修改(包括删除)
git add -A
# 或
git add --all

# 交互式添加
git add -i

# 添加部分修改(交互式选择块)
git add -p <file-name>

提交更改

# 提交暂存区的更改
git commit -m "提交信息"

# 提交并添加所有已跟踪文件的修改
git commit -am "提交信息"

# 修改最后一次提交
git commit --amend

# 修改最后一次提交并保留提交信息
git commit --amend --no-edit

查看差异

# 查看工作区与暂存区的差异
git diff

# 查看暂存区与最新提交的差异
git diff --cached
# 或
git diff --staged

# 查看工作区与最新提交的差异
git diff HEAD

# 查看指定文件的差异
git diff <file-name>

# 查看两次提交之间的差异
git diff <commit1> <commit2>

删除与重命名

# 删除文件(同时从工作区和暂存区移除)
git rm <file-name>

# 仅停止跟踪,保留工作区文件
git rm --cached <file-name>

# 重命名文件
git mv <old-name> <new-name>

三、分支管理

查看分支

# 查看本地分支
git branch

# 查看远程分支
git branch -r

# 查看所有分支
git branch -a

# 查看分支详细信息(含最新提交)
git branch -v

# 查看已合并到当前分支的分支
git branch --merged

# 查看未合并到当前分支的分支
git branch --no-merged

# 根据 commit hash 查询包含该提交的分支
git branch -a --contains <commit-hash>

创建与切换分支

# 创建新分支
git branch <branch-name>

# 创建并切换到新分支
git checkout -b <branch-name>
# 或(Git 2.23+ 推荐)
git switch -c <branch-name>

# 切换到已有分支
git checkout <branch-name>
# 或
git switch <branch-name>

# 切换到上一个分支
git checkout -
# 或
git switch -

# 基于远程分支创建本地分支
git checkout -b <local-branch> origin/<remote-branch>

合并分支

# 合并指定分支到当前分支
git merge <branch-name>

# 合并并生成合并提交(即使可以快进)
git merge --no-ff <branch-name>

# 合并并压缩为一个提交
git merge --squash <branch-name>

# 中止合并
git merge --abort

变基操作

# 将当前分支变基到指定分支
git rebase <branch-name>

# 交互式变基(修改历史)
git rebase -i <commit-hash>

# 继续变基(解决冲突后)
git rebase --continue

# 跳过当前提交
git rebase --skip

# 中止变基
git rebase --abort

挑选提交

# 将指定提交应用到当前分支
git cherry-pick <commit-hash>

# 挑选多个提交
git cherry-pick <commit1> <commit2>

# 挑选并保留提交信息
git cherry-pick -x <commit-hash>

删除分支

# 删除已合并的本地分支
git branch -d <branch-name>

# 强制删除本地分支
git branch -D <branch-name>

# 删除远程分支
git push origin --delete <branch-name>
# 或
git push origin :<branch-name>

四、远程操作

远程仓库管理

# 查看远程仓库
git remote -v

# 添加远程仓库
git remote add <name> <url>

# 重命名远程仓库
git remote rename <old-name> <new-name>

# 删除远程仓库
git remote remove <name>

# 查看远程仓库详细信息
git remote show <name>

# 更新远程分支信息
git remote update

抓取与拉取

# 从远程获取最新分支信息(不合并)
git fetch origin

# 获取所有远程仓库的更新
git fetch --all

# 拉取并合并当前分支的远程更新
git pull

# 拉取并变基
git pull --rebase

# 拉取指定远程分支
git pull origin <branch-name>

推送代码

# 推送当前分支到远程
git push origin <branch-name>

# 推送并建立追踪关系
git push -u origin <branch-name>

# 推送所有分支
git push --all origin

# 推送标签
git push origin <tag-name>

# 推送所有标签
git push origin --tags

# 强制推送(谨慎使用)
git push -f
# 或
git push --force

# 安全的强制推送(仅当远程没有新提交时)
git push --force-with-lease

# 将删除操作提交到远程
git push origin master -f

五、撤销与恢复

撤销工作区修改

# 丢弃某个文件的修改(工作区)
git checkout -- <file-name>

# 丢弃所有工作区修改
git checkout -- .

# 恢复文件到指定提交版本
git checkout <commit-hash> -- <file-name>

# 使用 restore 恢复文件(Git 2.23+)
git restore <file-name>

# 恢复所有文件
git restore .

撤销暂存区修改

# 将文件从暂存区撤出(保留工作区修改)
git reset HEAD <file-name>

# 撤出所有暂存区文件
git reset HEAD

# 使用 restore(Git 2.23+)
git restore --staged <file-name>

撤销提交

# 撤销上一次提交,保留修改到工作区
git reset HEAD^

# 撤销上一次提交,保留修改到暂存区
git reset --soft HEAD^

# 撤销上一次提交,丢弃所有修改(危险!)
git reset --hard HEAD^

# 回退到指定版本(保留工作区)
git reset <commit-hash>

# 回退到指定版本(丢弃所有修改)
git reset --hard <commit-hash>

# 使用 revert 创建反向提交(安全撤销)
git revert <commit-hash>

# 撤销多次提交
git revert <oldest-commit-hash>^..<newest-commit-hash>

误删恢复

# 查看所有操作记录(包括已删除的提交)
git reflog

# 复制要恢复操作的前面的 hash 值,然后执行
git reset --hard <hash>

# 恢复已删除的分支
git reflog show <branch-name>

清理未跟踪文件

# 查看将被清理的文件(干运行)
git clean -n

# 删除未跟踪的文件
git clean -f

# 删除未跟踪的文件和目录
git clean -fd

# 删除被忽略的文件
git clean -fx

六、储藏与清理

储藏修改

# 储藏当前工作区和暂存区的修改
git stash

# 储藏并添加描述
git stash save "描述信息"

# 储藏包括未跟踪的文件
git stash -u
# 或
git stash --include-untracked

# 储藏时保留暂存区状态
git stash --keep-index

查看与恢复储藏

# 查看储藏列表
git stash list

# 应用最近一次的储藏(不删除)
git stash apply

# 应用指定储藏
git stash apply stash@{n}

# 弹出最近一次的储藏(应用并删除)
git stash pop

# 弹出指定储藏
git stash pop stash@{n}

# 查看储藏的详细差异
git stash show -p

# 删除最近一次的储藏
git stash drop

# 删除指定储藏
git stash drop stash@{n}

# 清空所有储藏
git stash clear

七、标签管理

创建标签

# 创建轻量标签
git tag <tag-name>

# 创建附注标签
git tag -a <tag-name> -m "标签说明"

# 基于指定提交创建标签
git tag -a <tag-name> <commit-hash> -m "标签说明"

查看与推送标签

# 查看所有标签
git tag

# 查看标签详细信息
git show <tag-name>

# 推送标签到远程
git push origin <tag-name>

# 推送所有标签
git push origin --tags

删除标签

# 删除本地标签
git tag -d <tag-name>

# 删除远程标签
git push origin --delete <tag-name>

八、日志与查看

查看提交历史

# 查看提交历史
git log

# 简洁显示(一行一个提交)
git log --oneline

# 图形化显示分支合并历史
git log --graph --oneline --all

# 查看指定文件的修改历史
git log -p <file-name>

# 查看指定作者的提交
git log --author="name"

# 查看指定时间范围的提交
git log --since="2024-01-01" --until="2024-12-31"

# 查看最近 N 次提交
git log -n 5

# 查看提交统计信息
git log --stat

查看提交详情

# 查看指定提交的详细信息
git show <commit-hash>

# 查看指定文件在指定提交的版本
git show <commit-hash>:<file-name>

# 查看文件逐行修改信息(追责)
git blame <file-name>

# 查看文件在指定行的修改信息
git blame -L 10,20 <file-name>

查看引用日志

# 查看 HEAD 的操作历史
git reflog

# 查看指定分支的引用日志
git reflog show <branch-name>

九、补丁操作

生成补丁

# 将工作区与版本库的差异做成补丁
git diff HEAD > patch.diff

# 将暂存区与版本库的差异做成补丁
git diff --cached > patch.diff

# 将指定提交的差异做成补丁
git format-patch -1 <commit-hash>

# 将多次提交做成补丁文件
git format-patch <commit-hash>^..<commit-hash>

应用补丁

# 检查 patch/diff 能否正常打入
git apply --check file.patch

# 打入 patch/diff
git apply patch.diff

# 使用 am 应用补丁(保留提交信息)
git am patch

# 如果在合入 patch 的过程中报错了
git apply --reject file.patch

# 解决冲突后继续 am
git am --continue

# 跳过当前补丁
git am --skip

# 中止补丁应用
git am --abort

十、高级技巧

停止跟踪文件

# 停止跟踪文件(本地修改不再显示为未暂存)
git update-index --assume-unchanged <文件路径>

# 恢复跟踪
git update-index --no-assume-unchanged <文件路径>

# 查看被标记为 assume-unchanged 的文件
git ls-files -v | grep "^[a-z]"

二分查找(Bisect)

# 开始二分查找
git bisect start

# 标记当前提交为有问题
git bisect bad

# 标记某个提交为正常
git bisect good <commit-hash>

# Git 会自动切换到中间提交,测试后标记
git bisect good  # 或 git bisect bad

# 结束二分查找
git bisect reset

子模块

# 添加子模块
git submodule add <repository-url> <path>

# 初始化子模块
git submodule init

# 更新子模块
git submodule update

# 递归克隆包含子模块的仓库
git clone --recurse-submodules <repository-url>

工作区保存

# 保存当前工作进度(包括暂存区)
git stash push -m "工作描述"

# 创建并切换到新分支,同时弹出储藏
git stash branch <branch-name> stash@{0}

搜索

# 在提交历史中搜索内容
git log -S "搜索内容"

# 在提交历史中搜索正则表达式
git log -G "正则表达式"

# 在所有文件中搜索内容
git grep "搜索内容"

归档

# 将仓库打包为 tar.gz
git archive --format=tar.gz -o archive.tar.gz HEAD

# 只打包指定目录
git archive --format=zip -o archive.zip HEAD:src/

附录:常用别名推荐

~/.gitconfig 中添加:

[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    di = diff
    lg = log --graph --oneline --all --decorate
    last = log -1 HEAD
    unstage = reset HEAD --
    discard = checkout --
    amend = commit --amend
    visual = !gitk

提示:使用 git <command> --helpgit help <command> 可查看任何命令的详细文档。


文档整理时间:2026-08-14

posted @ 2026-08-15 21:01  木头人吃菜不吃饭  阅读(2)  评论(0)    收藏  举报