【git stash切换】

问题

当前正在修改对应某个bug,突然来了个更紧急的工作,需要保留现场,去对应更紧急的事务,git该如何操作?

1. 查看当前工作状态(确认修改)

git status

2. 保存当前工作现场(包含未提交的修改)

git stash save "WIP: Bugfix for issue #123" # 添加描述性信息

3. 确认工作区已干净

git status # 应显示"working tree clean"

4. 切换到紧急任务分支

git checkout main # 或创建新分支 git checkout -b hotfix-xxx

5. 处理紧急任务(修改、提交等)

... 进行紧急修改 ...

git add .

git commit -m "紧急修复XX问题"

6. 完成紧急任务后,回到原分支

git checkout original-branch

7. 恢复之前保存的工作现场

git stash pop # 恢复并删除最近的stash

查看存储列表

bash 复制代码
git stash list
bash 复制代码
ubuntu@ubuntuxxxxxx$ git stash list
stash@{0}: On hotfix-0529: save as leds
stash@{1}: On pdx257_fac_0523: bug for 0529 issue read

选择特定存储恢复

  1. 恢复但不删除
bash 复制代码
git stash apply stash@{1}
  1. 恢复并删除
bash 复制代码
git stash pop stash@{1}

最佳实践

bash 复制代码
[ 当前工作状态 ] 
   ↓
git stash save "描述"  → [ 安全存储箱 ]
   ↓ 
[ 干净工作区 ] → 处理紧急任务 → 提交
   ↓ 
git stash pop 
   ↓ 
[ 恢复工作现场 ]
相关推荐
vibecoding日记13 小时前
为什么我就想要「线性历史 + Signed Commits」,GitHub 却把我当猴耍 🤬🎙️
git·编程工具
程序员小崔日记1 天前
如何将代码轻松上传到 Gitee?Git 使用全攻略!
git·gitee·上传
Bigger2 天前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP3 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
一个人旅程~3 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
Factory_Audit3 天前
亚马逊社会责任验厂审核标准及注意事项
大数据·经验分享
江南小书生3 天前
制造业系统赋能成熟度自测表(实操版)
经验分享·非标制造
三流架构师3 天前
述职报告资源合集
经验分享
红豆子不相思3 天前
Tomcat 环境搭建与集群实战
服务器·git·tomcat