git找回删除的stash

本文介绍了如何在Git中恢复意外删除的stash。方法一是通过`git fsck --unreachable`命令找到丢失的commit,使用`git show`确认并用`git merge`恢复。方法二是利用IDEA的LocalHistory功能查看并回滚到历史状态。这两个方法可以帮助开发者找回误删的stash内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

不小心将刚stash的change删除了,导致之前的工作内容都没有了

分析和解决

方法一

通过git fsck --unreachable或者git fsck -- lost -found命令可以列出正常途径无法看见的节点,然后出现一个列表blob/commit/tree + 一串表示版本的字符。
由上而下可以看见最近的一次commit也就是那次stash的内容,可以通过git show "版本号" 查看确定是否是自己要找回的节点,忽略dangling blob 的类型的记录,只找 dangling commit 的记录。
确定之后用git merge “版本号”命令即可恢复到stash之前的状态。

方法二

利用IDEA自带的功能,

  1. 首先右键自己的项目或某个文件夹、或者某个文件,选择Local History -> Show History
  2. 然后就可以看到历史记录了
  3. 选择对应的记录右键Revert就可以了

参考资料

官网https://git-scm.com/docs/git-stash

Recovering stash entries that were cleared/dropped erroneously
If you mistakenly drop or clear stash entries, they cannot be recovered through the normal safety mechanisms. However, you can try the following incantation to get a list of stash entries that are still in your repository, but not reachable any more:

git fsck --unreachable |
grep commit | cut -d\  -f3 |
xargs git log --merges --no-walk --grep=WIP
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值