Skip to content

Git 命令速查表

快速查找常用 Git 命令,按使用场景分类


初始化与配置

bash
# 初始化新仓库
git init

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

# 全局配置
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

# 查看配置
git config --list

查看状态与历史

bash
# 查看状态
git status
git status -s              # 简洁模式

# 查看提交历史
git log
git log --oneline          # 单行显示
git log --oneline --graph  # 图形化分支
git log -n 5               # 最近 5 条

# 查看差异
git diff                   # 工作区 vs 暂存区
git diff --staged          # 暂存区 vs 最后提交
git diff <branch1> <branch2>

# 查看某次提交
git show <commit>

日常工作流

bash
# 添加到暂存区
git add <file>
git add .                  # 添加所有
git add -p                 # 交互式添加

# 提交
git commit -m "message"
git commit -am "message"   # add + commit (仅已追踪文件)
git commit --amend         # 修改上次提交

# 删除/移动文件
git rm <file>
git mv <old> <new>

分支操作

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

# 创建分支
git branch <name>
git switch -c <name>       # 创建并切换

# 切换分支
git switch <name>
git checkout <name>

# 合并分支
git merge <branch>

# 删除分支
git branch -d <name>       # 安全删除
git branch -D <name>       # 强制删除

# 重命名分支
git branch -m <new-name>

撤销操作

bash
# 撤销暂存
git restore --staged <file>

# 丢弃工作区修改(危险)
git restore <file>

# 重置到某个提交
git reset --soft HEAD~1    # 保留暂存区
git reset --mixed HEAD~1   # 保留工作区 (默认)
git reset --hard HEAD~1    # 全部丢弃(危险)

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

# 临时保存
git stash
git stash pop              # 恢复并删除
git stash list             # 查看列表
git stash apply            # 恢复但保留
git stash drop stash@{0}   # 删除指定

远程同步

bash
# 查看远程仓库
git remote -v
git remote add <name> <url>
git remote remove <name>

# 获取更新
git fetch                  # 只下载
git pull                   # 下载 + 合并
git pull --rebase          # 下载 + 变基

# 推送
git push
git push -u origin main    # 首次推送设置上游
git push --force           # 强制推送(危险)
git push --force-with-lease # 安全强制推送

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

高级操作

bash
# 变基
git rebase <branch>
git rebase -i HEAD~3       # 交互式变基
git rebase --abort         # 中止变基
git rebase --continue      # 解决冲突后继续

# 拣选提交
git cherry-pick <commit>

# 标签
git tag <name>             # 轻量标签
git tag -a v1.0 -m "msg"   # 附注标签
git push origin --tags     # 推送标签

# 二分查找 bug
git bisect start
git bisect bad             # 当前是坏的
git bisect good <commit>   # 这个是好的
git bisect reset           # 结束查找

# 清理未追踪文件
git clean -n               # 预览
git clean -f               # 执行删除(危险)

调试与检查

bash
# 查看某行代码谁写的
git blame <file>

# 搜索提交
git log --grep="keyword"
git log -S "code"          # 搜索代码变更

# 查看引用日志 (恢复删除的提交)
git reflog
git checkout <reflog-hash>

配置别名

bash
# 常用别名配置
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all"

紧急情况

情况命令
误删分支git reflog -> git checkout -b <name> <hash>
丢弃所有本地改动git reset --hard HEAD
撤销最近的提交但保留代码git reset --soft HEAD~1
撤销已推送的提交git revert <commit> -> git push
从历史中删除大文件git filter-branchbfg
中止合并/变基git merge --abort / git rebase --abort

提示:使用 git <command> --help 查看任何命令的详细帮助