# git stash 参考

URL: https://caijiao.org/reference/git/stash
Source: docs/reference/git/stash.md
Description: git stash Reference，整理临时保存改动、stash push、list、show、apply、pop、drop、include-untracked 和常见错误。

`git stash` 用于临时保存未提交改动，让工作区恢复干净。它适合临时切分支、拉取代码或处理紧急任务。

## 语法

```bash
git stash push -m "message"
```

## 常用命令

```bash
git stash
git stash push -m "wip: login form"
git stash list
git stash show -p stash@{0}
git stash apply stash@{0}
git stash pop
git stash drop stash@{0}
```

## 包含未跟踪文件

默认 `git stash` 不保存未跟踪文件。需要一起保存时：

```bash
git stash push -u -m "include untracked files"
```

## apply 和 pop 区别

| 命令 | 行为 |
| --- | --- |
| `git stash apply` | 应用 stash，但保留 stash 记录 |
| `git stash pop` | 应用 stash，成功后删除 stash 记录 |

## 常见错误

- 忘记 `-u`，导致新建文件没有进入 stash。
- stash 太多但没有 message，后续不知道每一条是什么。
- `pop` 出现冲突后，stash 不一定会删除，要看命令输出并处理冲突。

## 相关内容

- [Git Reference](/reference/git/)
- [git reset](/reference/git/reset)
- [Git 教程：常见排错](/git/practice/troubleshooting)
- [git-stash 官方文档](https://git-scm.com/docs/git-stash)
