【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 
   ↓ 
[ 恢复工作现场 ]
相关推荐
星纬智联技术17 分钟前
开源 AI-Eval:Prompt 评估系统,用单元测试跑
经验分享
zhyf11926 分钟前
AU软件安装详细步骤梳理(win&mac)
经验分享
AIGC小火龙果42 分钟前
【出海心路】Claude Code实战心法
经验分享
何中应8 小时前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应8 小时前
Git本地仓库命令补充
git
sun00770010 小时前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git
星哥说事10 小时前
跨平台打包神器,免费将网页、Vue、React秒变桌面 APP,仅需 1 个 Github Token!
经验分享
胖虎112 小时前
Git 一个本地仓库同时推送到两个远程仓库(详细教程)
git·多远程仓库·双远程仓库·git双远程·git备份
sweetone12 小时前
LINN莲CLASSIK桌面音响微修
经验分享·音视频
晚霞的不甘14 小时前
揭秘 CANN 内存管理:如何让大模型在小设备上“轻装上阵”?
前端·数据库·经验分享·flutter·3d