贮藏Stash
你可能会说
改到一半要切去干别的,先把现在这些东西临时收起来。
把当前没做完的改动临时收起来,让工作区先恢复干净例如,首页改到一半却要切去修 Bug,可以先 Stash,处理完再恢复。刚新建且从未加入 Git 的文件默认不会被收起,操作前要看 git status;Stash 只适合短期中转,正式成果仍要提交 Commit。
组成结构 · Anatomy
🛠 工作区尚未完成修改的 index.html
stash push →
stash@{0} 首页改动尚未完成 ← 最新
stash@{1} WIP on main: a1b2c3d
stash@{2} WIP on main: 0f1e2d3
尚未 commit 的改动,可通过 stash 临时保存
最后一次 git stash push 保存的改动;未指定时 pop 默认取它
每次执行 git stash push 后,已有记录的编号依次增加
编号越大表示记录越早;应及时确认是否恢复或删除
常见变体 · Variants
保存git stash push
git stash push -m "首页改动尚未完成"
需要暂存改动后切换任务;不会推送到远程
查看List
git stash list
查看已有的 stash 记录
恢复Apply
git stash apply stash@{0}
先恢复但保留备份,确认后再 drop
应用并保留Apply
git stash apply
恢复改动,同时保留 stash 记录
典型使用场景
未完成改动时执行 stash push
$ git status
modified: index.html(改动尚未完成)
⚡ 正式环境登录失败,需要切换到 hotfix/login
$ git stash push -m "首页改动尚未完成"
Saved working directory and index state On main: 首页改动尚未完成
$ git checkout hotfix/login
Switched to branch 'hotfix/login'
stash list 查看暂存清单
$ git stash list
stash@{0}: On main: 首页改动尚未完成
stash@{1}: WIP on main: a1b2c3d 首页基础版本
编号越小越新,pop 默认取 stash@{0}
stash apply 恢复
$ git checkout main
Switched to branch 'main'
$ git stash apply stash@{0}
On branch main
Changes not staged for commit:
modified: index.html
Changes restored to the working tree
改动已恢复,确认无误后再执行 drop
确认后 drop 清理
🛠 首页改动尚未完成
stash push →
📦 stash@{0}
→ 处理其他任务 →
🛠 stash apply 恢复
延伸阅读 · 权威出处