【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
查看存储列表
git stash list
ubuntu@ubuntuxxxxxx$ git stash list
stash@{0}: On hotfix-0529: save as leds
stash@{1}: On pdx257_fac_0523: bug for 0529 issue read
选择特定存储恢复
- 恢复但不删除
git stash apply stash@{1}
- 恢复并删除
git stash pop stash@{1}
最佳实践
[ 当前工作状态 ] ↓
git stash save "描述" → [ 安全存储箱 ]↓
[ 干净工作区 ] → 处理紧急任务 → 提交↓
git stash pop ↓
[ 恢复工作现场 ]