人人都会AI编程

夯爆了!2026年最新Git 指令大全

更新时间:2026-07-11

<span style="font-family:inherit;font-size:16px;font-weight:400;">本文按功能场景分类整理,覆盖日常开发 99% 的使用场景,便于速查与系统学习。</span>

一、基础配置

1. 用户信息配置

# 设置全局用户名
git config --global user.name "Your Name"

# 设置全局邮箱
git config --global user.email "your@email.com"

# 设置当前仓库用户名(不带--global)
git config user.name "Local Name"

# 查看配置
git config --list
git config user.name

2. 编辑器与别名

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

# 设置命令别名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm "commit -m"
git config --global alias.lg "log --oneline --graph --all"

3. 换行符与编码

# Windows 推荐:提交时转 LF,检出时转 CRLF
git config --global core.autocrlf true

# Mac/Linux 推荐:提交时转 LF,检出不转换
git config --global core.autocrlf input

# 禁止自动转换
git config --global core.autocrlf false

二、仓库初始化与克隆

# 在当前目录初始化仓库
git init

# 初始化并指定默认分支名
git init -b main

# 克隆远程仓库到本地
git clone <仓库地址>

# 克隆到指定目录
git clone <仓库地址> <目录名>

# 只克隆指定分支
git clone -b <分支名> --single-branch <仓库地址>

# 浅克隆(只拉最近一次提交,加快速度)
git clone --depth 1 <仓库地址>

三、文件暂存与提交

1. 查看状态

# 查看工作区与暂存区状态
git status

# 精简状态显示
git status -s

2. 添加文件到暂存区

# 添加指定文件
git add <文件名>

# 添加所有修改的文件
git add .

# 添加所有 .txt 文件
git add *.txt

# 交互式添加
git add -i

# 只添加已跟踪文件的修改(不包含新增文件)
git add -u

3. 提交代码

# 提交并写提交信息
git commit -m "提交说明"

# 追加提交(修改最近一次提交信息)
git commit --amend -m "新的提交说明"

# 追加提交且不修改提交信息
git commit --amend --no-edit

# 跳过暂存区,直接提交已跟踪文件
git commit -a -m "提交说明"

# 空提交(触发 CI 等场景)
git commit --allow-empty -m "trigger ci"

4. 删除与移动文件

# 删除文件并放入暂存区
git rm <文件名>

# 停止跟踪但保留本地文件
git rm --cached <文件名>

# 重命名/移动文件
git mv <旧名> <新名>

四、查看历史与差异

1. 提交历史

# 查看完整提交历史
git log

# 一行显示提交记录
git log --oneline

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

# 显示最近 n 条
git log -n 5

# 按作者筛选
git log --author="name"

# 按日期筛选
git log --since="2024-01-01" --until="2024-12-31"

# 搜索提交信息
git log --grep="关键词"

# 查看某个文件的修改历史
git log -- <文件路径>
git log -p <文件路径>

2. 查看差异 diff

# 工作区 vs 暂存区
git diff

# 暂存区 vs 最新提交
git diff --cached
git diff --staged

# 工作区 vs 最新提交
git diff HEAD

# 比较两个提交
git diff <commit1> <commit2>

# 比较两个分支
git diff <分支1> <分支2>

# 只显示文件名
git diff --name-only

3. 查看提交详情

# 查看某次提交的详细改动
git show <commit-hash>

# 查看最近一次提交
git show HEAD

# 查看某次提交的某个文件
git show <commit>:<文件路径>

五、分支管理

1. 查看分支

# 查看本地分支
git branch

# 查看所有分支(本地+远程)
git branch -a

# 查看远程分支
git branch -r

# 查看分支最后一次提交
git branch -v

2. 创建与切换

# 创建分支
git branch <分支名>

# 切换分支
git checkout <分支名>
git switch <分支名>  # Git 2.23+ 推荐

# 创建并切换
git checkout -b <分支名>
git switch -c <分支名>

# 基于指定提交创建分支
git checkout -b <分支名> <commit-hash>

3. 删除分支

# 删除已合并的分支
git branch -d <分支名>

# 强制删除(未合并也删)
git branch -D <分支名>

# 删除远程分支
git push origin --delete <分支名>
git push origin :<分支名>

4. 分支重命名

# 重命名当前分支
git branch -m <新名字>

# 重命名指定分支
git branch -m <旧名> <新名>

六、远程仓库操作

1. 关联远程仓库

# 查看远程仓库
git remote -v

# 添加远程仓库
git remote add origin <仓库地址>

# 修改远程地址
git remote set-url origin <新地址>

# 删除远程仓库
git remote remove origin

# 重命名远程
git remote rename old new

2. 拉取与推送

# 拉取远程分支并合并
git pull
git pull origin <分支名>

# 拉取并变基(推荐,更整洁)
git pull --rebase

# 推送本地分支到远程
git push
git push origin <分支名>

# 推送并建立上游关联
git push -u origin <分支名>

# 强制推送(谨慎使用)
git push --force
git push --force-with-lease  # 安全强制推送

3. 抓取远程更新

# 抓取所有远程更新但不合并
git fetch

# 抓取指定远程
git fetch origin

# 清理远程已删除的分支引用
git fetch -p
git remote prune origin

七、撤销与回退

1. 撤销工作区修改

# 丢弃工作区单个文件修改
git checkout -- <文件>
git restore <文件>  # Git 2.23+

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

2. 撤销暂存区

# 把文件从暂存区撤回工作区
git reset HEAD <文件>
git restore --staged <文件>  # Git 2.23+

# 全部撤回
git reset HEAD

3. 回退提交

# 软回退:移动 HEAD,保留修改到暂存区
git reset --soft <commit>

# 混合回退(默认):移动 HEAD,保留修改到工作区
git reset --mixed <commit>
git reset <commit>

# 硬回退:彻底丢弃所有修改
git reset --hard <commit>

# 回退到上一次提交
git reset --hard HEAD^
git reset --hard HEAD~1

# 回退 n 次
git reset --hard HEAD~n

4. 反向提交(安全回滚)

# 创建一个新提交来撤销指定提交
git revert <commit-hash>

# 撤销最近一次提交
git revert HEAD

# 不自动提交,只放到暂存区
git revert -n <commit-hash>

八、储藏(Stash)

# 暂存当前工作进度
git stash
git stash save "说明文字"

# 暂存包含未跟踪文件
git stash -u
git stash --include-untracked

# 查看储藏列表
git stash list

# 应用最近一次储藏并删除
git stash pop

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

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

# 清空所有储藏
git stash clear

# 查看储藏内容
git stash show stash@{0}
git stash show -p stash@{0}

九、合并与变基

1. 合并 merge

# 把指定分支合并到当前分支
git merge <分支名>

# 不使用快进模式,生成合并提交
git merge --no-ff <分支名>

# 只进行快进合并
git merge --ff-only <分支名>

# 中止合并
git merge --abort

# 解决冲突后继续合并
git merge --continue

2. 变基 rebase

# 将当前分支变基到目标分支
git rebase <目标分支>

# 交互式变基(整理提交)
git rebase -i <commit>
git rebase -i HEAD~n

# 中止变基
git rebase --abort

# 跳过当前冲突继续
git rebase --skip

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

3. 拣选提交

# 把某次提交应用到当前分支
git cherry-pick <commit-hash>

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

# 拣选一段范围
git cherry-pick A..B

# 只应用改动不自动提交
git cherry-pick -n <commit>

十、标签管理

# 列出所有标签
git tag

# 创建轻量标签
git tag v1.0.0

# 创建附注标签(推荐,带信息)
git tag -a v1.0.0 -m "Release 1.0.0"

# 给历史提交打标签
git tag -a v1.0.0 <commit-hash>

# 查看标签信息
git show v1.0.0

# 删除本地标签
git tag -d v1.0.0

# 推送标签到远程
git push origin v1.0.0

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

# 删除远程标签
git push origin --delete v1.0.0
git push origin :refs/tags/v1.0.0

十一、高级操作

1. Bisect 二分查找 bug

# 开始二分查找
git bisect start

# 标记当前版本为坏
git bisect bad

# 标记某个版本为好
git bisect good <commit>

# 重置退出
git bisect reset

2. Blame 逐行追溯

# 查看文件每行最后修改人
git blame <文件>

# 查看指定行范围
git blame -L 10,20 <文件>

3. 清理与优化

# 清理无用对象
git gc

# 查看仓库大小
git count-objects -vH

# 清理未跟踪文件
git clean -n    # 预览
git clean -f    # 删除文件
git clean -fd   # 删除文件和目录
git clean -fx   # 包括忽略的文件

4. 子模块 submodule

# 添加子模块
git submodule add <仓库地址> <路径>

# 初始化并更新子模块
git submodule init
git submodule update

# 克隆时递归拉取子模块
git clone --recursive <仓库地址>

# 更新所有子模块
git submodule update --remote

十二、常用速查表

场景
命令

放弃工作区所有修改
git checkout .

撤销最近一次提交且保留代码
git reset --soft HEAD~1

强制同步远程分支
git reset --hard origin/main

临时存代码去改 bug
git stash / git stash pop

把 A 分支的某个提交拿到 B 分支
git cherry-pick <hash>

整理杂乱的提交记录
git rebase -i HEAD~n

查看谁改了某行代码
git blame <file>

回滚已推送到远程的提交
git revert <hash> 然后 push