【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 
   ↓ 
[ 恢复工作现场 ]
相关推荐
会飞的小蛮猪3 小时前
Jenkins运维之路(初识流水线)
经验分享
程序媛Dev3 小时前
50.4k Star!我用这个神器,在五分钟内搭建了一个私有 Git 服务器!
运维·服务器·git
晨非辰4 小时前
#C语言——刷题攻略:牛客编程入门训练(九):攻克 分支控制(三)、循环控制(一),轻松拿捏!
c语言·开发语言·经验分享·学习方法·visual studio
TeleostNaCl5 小时前
OpenWrt | 在 PPP 拨号模式下启用 IPv6 功能
网络·经验分享·智能路由器·ip
澈轩6 小时前
Git 用得好,下班走得早
git
人间造梦工厂7 小时前
Git Bash 别名
git
画个太阳作晴天12 小时前
解决 Android Studio 中 build 目录已被 Git 跟踪后的忽略问题
git
晓衣15 小时前
2025“獬豸杯”全国电子数据取证竞赛-k8s服务器取证wp
服务器·经验分享·程序人生·网络安全·容器·kubernetes·学习方法
wjs04016 小时前
Git常用的命令
java·git·gitlab
原野风霜32417 小时前
Git使用总结
git