Git stash 暂存现场指南
什么时候用
当你做到一半需要临时切换分支、处理紧急任务,或者现在还不想提交一堆半成品改动时,可以先把现场放进 stash。
先判断当前状态
git status --short
git stash list
git status --short用来确认当前有哪些改动。git stash list用来确认仓库里已经存了哪些现场。
推荐命令
| 场景 | 推荐命令 | 结果 |
|---|---|---|
| 暂存已跟踪改动 | git stash push -m "wip: message" | 保存当前现场,并恢复干净工作区 |
| 连未跟踪文件一起暂存 | git stash push -u -m "wip: message" | 额外包含新文件 |
| 查看 stash 列表 | git stash list | 查看所有暂存现场 |
| 查看某个 stash 具体改了什么 | git stash show -p stash@{0} | 显示差异 |
| 恢复并删除 stash | git stash pop | 应用成功后自动移除该 stash |
| 恢复但保留 stash | git stash apply stash@{0} | 方便反复尝试 |
| 删除某个 stash | git stash drop stash@{0} | 手动清理不用的现场 |