git 错误reset的解决

昨天在公司上班的时候, 因为旧版本临时有了一些改动, 需要修改另一个分支上的代码.

boss在我电脑上看了下, 说: “这些改动可以 reset 吧? 好像用不到? 你到另一个分支去改一下东西.”

我当时脑子有点懵, 嗯了一下, 然后亲眼看着他reset, 然后checkout, 然后我接过键盘开始改…

两个小时后, 当我转回来继续之前的工作时…

妈呀, 我的天, 刚才发生了什么! @%&$%& 苍天呐, 我写了两天的代码, 而且还有一些没提交的啊! 我也不知道我当时怎么了, 竟然 ? 你特么这都敢

唉, 别提了, 自己挖的坑, 还是自己躺进去吧

先git reflog查看一下最近的记录

$ git reflog

log信息

找到了我之前的一次提交 26f8b7f HEAD@{18}, 还好, 情况不算太糟

执行reset的操作回到之前的进度

$ git reset --hard 26f8b7f

赶紧回到工作目录把之前的改动copy一份到别的地方压压惊.

OK, 继续…

当时我还有一些没有commit的代码改动. 我也不确定当时缓存了没有, 到stackflow上查了一下, 对我用处比较大的回答是下面这个, 感谢:

You can recover anything you git add ed, with git fsck --lost-found and poke around in .git/lost-found. find .git/objects -type f | xargs ls -lt | sed 60q will give you the last 60 things to get added to the repo, that’ll help. Anything you didn’t git add is gone as surely as if you’d deleted it yourself.

意思就是说, 如果你缓存过该文件, 即对该文件执行过 git add, 则可以找到它.

如果没有, 那你不用往下看了, 去墙角哭吧.

我也不确定我当时缓存了没有(哭, 先试试看吧, 上帝保佑…

执行下面的命令查看缓存记录

$ find .git/objects -type f | xargs ls -lt | sed 20q

这里写图片描述

编写shell脚本将缓存输出到txt文件

git cat-file -p 17418dbd64ef539b8d7d34b61f357aedb67129e4 > ~/Desktop/logs/a.txt
git cat-file -p 318d8a6f000dfe1f963c42ba004b67ac6e5f6d66 > ~/Desktop/logs/b.txt
git cat-file -p b14fd3e35c446d864c810f9f62f3d631c636bf83 > ~/Desktop/logs/c.txt
git cat-file -p cbe6c25748dbd0516f7a1b953e395fdf3fb9d428 > ~/Desktop/logs/d.txt
git cat-file -p 4d5767e2dcafa92f63705ce168a0956bcec1164f > ~/Desktop/logs/e.txt
git cat-file -p ede2be9626f48e2d453db51932f5afc356041f9c > ~/Desktop/logs/f.txt
git cat-file -p 0496b89720ff1e0b3fd543a4532fe12eeb27a843 > ~/Desktop/logs/g.txt
git cat-file -p 079329114f88c1a6353aeb383e356e9b967a97e9 > ~/Desktop/logs/h.txt
git cat-file -p 08b83d4667f8ce81570420827d8702aeb2f4f16f > ~/Desktop/logs/i.txt
git cat-file -p 0b486919ae98d1e594f467597453e42c1859183a > ~/Desktop/logs/j.txt
git cat-file -p 0c7a3f9a3460a050831d7464a4197ee80c69b203 > ~/Desktop/logs/k.txt
git cat-file -p 11407e2df6111b2cec0c47614aac9820ecf3ecb1 > ~/Desktop/logs/l.txt
git cat-file -p 204212c5f3124dc3780619d41f09035d019e4625 > ~/Desktop/logs/m.txt
git cat-file -p 2be64c7184c8a42804f0a588a57913c2b2e94bbc > ~/Desktop/logs/n.txt
git cat-file -p 3400f560fe2af65e912fdd298643ae60fc9a173d > ~/Desktop/logs/o.txt
git cat-file -p 370afee3743fed8e78649b81b807a532f79a592a > ~/Desktop/logs/p.txt
git cat-file -p 40bfaf7c91091895af3024a9aa39566a4fb53860 > ~/Desktop/logs/q.txt
git cat-file -p 43e698eae9b2788f1e8982a74e14c2dc51c4897d > ~/Desktop/logs/r.txt
git cat-file -p 443bd13f0fba720c7ef895c7a54867df76d58b2a > ~/Desktop/logs/s.txt
git cat-file -p 492b55ec0287fc31588cd87a6ef12a2b9b6ef57e > ~/Desktop/logs/t.txt

你们看到-p后面那一串东西有没有很眼熟? 是的没错, 是上一张图片的东西, 其实就是下面绿色框出来的部分去掉”/”

这里写图片描述

运行上面的脚本, 将缓存导出到text文件, 也可以直接在terminal执行上面的语句, 然后我边祈祷边开始浏览…

a.txt没有…

b.txt没有…

c.txt也没有…

d.txt也没有…

e.txt也没有…

就在我要绝望的时候… !!!! 天呐, 这熟悉的代码是什么! 这不就是我失眠了到四点还在想的代码吗?!

这里写图片描述

瞬间找回工作热情╮(╯▽╰)╭
果然要养成随手 git add . 的习惯

The End

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值