用 git stash 暂存和恢复未提交的修改
把写到一半的改动收起来,切分支或拉取代码,回来一条命令恢复。
执行 git stash push -m "wip: login fix" 暂存当前改动,回来后用 git stash pop 恢复最近一次暂存。
01命令
命令
$ git stash push -m "wip: login fix"02作用说明
git stash 会把工作区和暂存区整体拍个快照,然后重置到 HEAD,不会产生提交。暂存保存在一个栈上(stash@{0} 是最新的),git stash list 可以查看全部,stash apply 恢复但不删除,pop 则恢复后删除。加 -m 写个说明,多条暂存才不会搞混,比默认的匿名暂存清楚得多。
03风险
!注意: 未跟踪的新文件默认不会被暂存,要加 -u(git stash push -u)——这是“新文件丢了”的常见乌龙,其实文件还在磁盘上。pop 遇到冲突时暂存不会被删除,解决冲突并提交后需手动 drop;而且 stash 只在本地存在,绝不能当备份用。
04撤销
撤销
$ git stash pop05来源
- git-stash - Official Git documentation依据: Official docs for git stash push/list/pop/apply/drop, including stash@{0} refs and -m message handling.
- git-stash(1) — Debian bookworm依据: Documents git stash push with -m message, plus list, show, pop, apply and drop subcommands.
最后验证: 2026-09-19 · 分类: 开发者