git reset —hard后暂存区代码跑哪儿去了?

准备提交代码到远程Git,当我Pull远程代码的时候提示我有冲突,主要是resource目录下的文件被删除的更新,奈何IDEA又不给我个合并的界面,只告诉我这几个文件需要合并~git reset —hard后暂存区代码跑哪儿去了?于是我脑袋一热就回滚到有需要合并的那个commit版本,然后就fuck,我暂存区的代码全部“game over!”不见了。

解决方案

发现代码不见了顿时就像第一次见了喜欢的姑娘,心里那是一波又一波的小鹿乱撞,不过这是悲伤的。

于是乎就在网上急切的寻找办法,终于在我一顿操作猛如虎的搜索下,最终将代码恢复了。

在仓库的目录下打开终端,输入:

<span class="hljs-built_in">find</span> .git/objects -<span class="hljs-built_in">type</span> f | xargs ls -lt | sed <span class="hljs-number">60</span>q

其中末尾的60q代表最近60次的add操作,然后会出来很多类似于这样的记录:

<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>      <span class="hljs-number">249</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:59</span> <span class="hljs-string">.git/objects/c0/dd72f08b16a4f9c1d87b836b7ecee75a332252</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>       <span class="hljs-number">45</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:59</span> <span class="hljs-string">.git/objects/58/c50797f9fe9dceda109019a2b8d9cf18a48df4</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>       <span class="hljs-number">46</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:59</span> <span class="hljs-string">.git/objects/9c/9c6c686a21fc394d8dfc550f41824205b1dffa</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>      <span class="hljs-number">750</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:59</span> <span class="hljs-string">.git/objects/d7/73c7e375a316fe3e00800ffd5c5be49b00fba3</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>     <span class="hljs-number">1217</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:59</span> <span class="hljs-string">.git/objects/6b/a44cf7a71f83f06d969af0a2464451edc133bb</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>    <span class="hljs-number">29067</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:57</span> <span class="hljs-string">.git/objects/1a/56f322c6cbe07642ca248cc8de3a63d5acd0d7</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>      <span class="hljs-number">200</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:55</span> <span class="hljs-string">.git/objects/30/3d28198f9375111092438ddb7d872aca84f863</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>       <span class="hljs-number">79</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:55</span> <span class="hljs-string">.git/objects/ab/0c251513237d54b9c439e73a2f26486432ed3d</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>      <span class="hljs-number">234</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:55</span> <span class="hljs-string">.git/objects/e4/57508e691703f4ff58a69f3410cd28f3780d63</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>       <span class="hljs-number">45</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:55</span> <span class="hljs-string">.git/objects/67/948e2e1cb35a82799585b64859d8473b01dbe7</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>       <span class="hljs-number">47</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:55</span> <span class="hljs-string">.git/objects/f5/4b5612be0810f4658c15a9a2297ac34198c7e0</span>
<span class="hljs-string">-r--r--r--</span>  <span class="hljs-number">1</span> <span class="hljs-string">licoy</span>  <span class="hljs-string">staff</span>      <span class="hljs-number">250</span> <span class="hljs-string">Jun</span> <span class="hljs-number">17</span> <span class="hljs-number">17</span><span class="hljs-string">:39</span> <span class="hljs-string">.git/objects/1a/908f631cfe95ab0979877f929280992bacc009</span>
<span class="hljs-string">...</span>

然后我们选择差不多最近的时间记录,然后找到他的文件路径在终端输入

<span class="hljs-attr">git</span> <span class="hljs-string">cat-file -p {commit_id} > c.txt</span>

其中{commit_id}.git/objects/ab/0c251513237d54b9c439e73a2f26486432ed3d部分中的ab/0c251513237d54b9c439e73a2f26486432ed3d,要去掉中间的斜杠,然后就是一次commit_id

执行上述命令之后会将commit_id主要内容输出到c.txt,然后我们需要人工辨别记录内容是不是我们暂存区的内容,如果是,那我们只需要在回滚到这个commit_id版本即可。

尾记

这次的操作之后让我对每次的回滚更加小心翼翼了,说白了还是对Git不是那么透彻,git reset --hard要慎用,用不好就像用了rm -rf xxx一样,后悔莫及哟~

 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

除特别注明外,本站所有文章均基于CC-BY-NC-SA 4.0原创,转载请注明出处。
文章名称:《git reset —hard后暂存区代码跑哪儿去了?》
文章链接:https://www.vvso.cn/xlbk/17718.html
分享到: 更多 (0)

热门文章

评论 抢沙发

切换注册

登录

忘记密码 ?

切换登录

注册