# Git 常用命令与紧急救援速查手册 (2026 珍藏版)

> 整理制作：[趁手 iKit](https://iktu.cn)  
> 在线可交互速查工具：https://iktu.cn/tools/git-cheatsheet

---

## 🚨 1. 致命误操作紧急救援 (Oh Shit, Git!)

### 找回误删分支或丢失的 commit
```bash
# 1. 查看操作历史和 HEAD 指针移动记录
git reflog
# 2. 找到分支被删前的那次提交哈希 (如 a1b2c3d)
# 3. 重新基于该 commit 拉出分支
git checkout -b <分支名> <commit-id>
```

### 刚提交完发现有文件漏加或 commit message 写错了
```bash
git add <漏掉的文件>
git commit --amend --no-edit   # 只补文件不改信息
# 或
git commit --amend -m "新的提交信息"
```

### 撤销最近一次 commit，但保留已写的代码修改
```bash
git reset --soft HEAD~1
```

### 彻底撤销未提交的改动，恢复干净工作区（慎用）
```bash
git restore . && git clean -fd
```

---

## 🌱 2. 常用工作流日常命令

### 分支操作
```bash
# 创建并切换分支
git checkout -b feature/awesome-feature
# 或 Git 新版推荐语法:
git switch -c feature/awesome-feature

# 列出所有本地和远程分支
git branch -a

# 删除本地分支
git branch -d feature/old-branch
```

### 提交与同步
```bash
# 添加所有变动并提交
git add .
git commit -m "feat: 增加新特性"

# 优雅拉取远端更新（避免自动生成无意义的 merge commit）
git pull --rebase origin main

# 推送分支并关联上游
git push -u origin feature/awesome-feature
```

---

## 📦 3. 临时暂存 (Stash) 技巧

```bash
# 暂存当前所有变动（包含未追踪的新文件 -u）
git stash -u -m "临时修复线上 bug 暂存"

# 查看暂存列表
git stash list

# 取出最近一次暂存并从暂存栈删除
git stash pop

# 取出指定暂存但不删除
git stash apply stash@{0}
```

---

## 🔍 4. 变基、代码捡选与比对

```bash
# 把其他分支上的某次提交单独拿到当前分支
git cherry-pick <commit-id>

# 互动式合并/修改最近 3 次提交
git rebase -i HEAD~3

# 查看文件的逐行修改者及 commit（抓背锅侠神器）
git blame -L 1,50 src/index.ts

# 查看尚未暂存的变动
git diff
```

---

## 🏷️ 5. 远程分支与标签管理

```bash
# 本地清理远端已删除的分支索引
git remote prune origin

# 打版本标签
git tag -a v1.0.0 -m "Release v1.0.0"
git push origin v1.0.0
```

---
*本文档免登录永久免费，更多实用开发者与办公工具请访问 [趁手 iKit (iktu.cn)](https://iktu.cn)*
