📖 Git stash 暂存速查

按使用场景分组,点击"复制"按钮一键复制命令

基础暂存与恢复
暂存所有已跟踪文件的改动(工作区变干净)
git stash
查看暂存列表(stash@{0} 为最新)
git stash list
恢复最近一次暂存并删除该记录
git stash pop
恢复最近一次暂存但保留记录(可多次应用)
git stash apply
删除最近一次暂存记录
git stash drop
清空所有暂存记录 ⚠️ 不可恢复
git stash clear
暂存选项
带说明消息暂存(推荐,替代已弃用的 save)
git stash push -m "修复登录bug"
暂存时包含未跟踪文件(默认不含)
git stash -u
暂存时包含被 .gitignore 忽略的文件
git stash -a
只暂存指定文件,其他改动保留在工作区
git stash push <file1> <file2>
已 git add 的部分保留在暂存区,只 stash 未 add 的改动
git stash --keep-index
交互式逐块选择哪些改动需要暂存
git stash -p
查看暂存内容
查看最近一次暂存改了哪些文件
git stash show
查看最近一次暂存的具体 diff
git stash show -p
查看指定暂存改了哪些文件(stash@{n})
git stash show "stash@{1}"
查看指定暂存的具体 diff
git stash show "stash@{1}" -p
操作指定暂存
恢复指定暂存并删除该记录
git stash pop "stash@{1}"
应用指定暂存但保留记录
git stash apply "stash@{1}"
删除指定暂存记录
git stash drop "stash@{1}"
从暂存创建新分支并应用改动(暂存记录自动删除)
git stash branch feature/login "stash@{0}"
注意事项与排错
临时保存去修紧急 bug:stash → 切分支 → 修完 pop 回来
git stash push -m "WIP" && git checkout hotfix && git stash pop
pop 冲突时:手动解决 → git add → git stash drop
git add . && git stash drop
stash 太多找不到?list + show 定位后再 pop 指定
git stash list && git stash show "stash@{2}" -p
PowerShell / zsh 中 stash@{n} 必须加引号,否则报语法错误
git stash pop "stash@{0}"