Git常用命令速查手册

Git 命令太多记不住?这份速查手册按使用场景分类,覆盖日常开发中 90% 的 Git 操作。

仓库初始化

  • git init 在当前目录初始化仓库
  • git clone <url> 克隆远程仓库到本地

日常提交

  • git status 查看工作区和暂存区状态
  • git add <file> 添加文件到暂存区
  • git add . 添加所有修改到暂存区
  • git commit -m "msg" 提交暂存区的修改
  • git commit --amend 修改最近一次提交的信息

查看历史

  • git log 查看提交历史
  • git log --oneline --graph 简洁的图形化日志
  • git diff 查看未暂存的修改
  • git diff --staged 查看已暂存未提交的修改
  • git show <commit> 查看某次提交的详细内容

分支管理

  • git branch 列出本地分支
  • git branch <name> 创建新分支
  • git checkout <branch> 切换分支
  • git checkout -b <branch> 创建并切换到新分支
  • git branch -d <branch> 删除已合并的分支
  • git branch -D <branch> 强制删除分支

合并与变基

  • git merge <branch> 将指定分支合并到当前分支
  • git rebase <branch> 将当前分支变基到指定分支
  • git rebase --abort 取消正在进行的变基
  • git cherry-pick <commit> 将某次提交应用到当前分支

远程操作

  • git remote -v 查看远程仓库地址
  • git push origin <branch> 推送分支到远程
  • git pull 拉取远程更新并合并
  • git fetch 仅拉取远程更新,不自动合并

暂存工作区

  • git stash 暂存当前修改
  • git stash list 查看暂存列表
  • git stash pop 恢复最近一次暂存并删除记录
  • git stash apply 恢复暂存但不删除记录

撤销与回退

  • git checkout -- <file> 撤销工作区的修改
  • git reset HEAD <file> 取消暂存
  • git reset --soft HEAD~1 撤销最近一次提交,保留修改在暂存区
  • git reset --hard HEAD~1 撤销最近一次提交,丢弃所有修改(慎用)
  • git revert <commit> 创建一个新提交来撤销指定提交