日常开发------git慎用git reset hard

今天因为pull失败,根据git的提示用了git reset hard,然后自己本地新写的代码全没了。

192: huehn$ git reset --hard
HEAD is now at d4cc15a #提交动态代理
192: huehn$ 

版本回退到了d4cc15a。我没有add的那些文件全不见了。然后我上网搜了一下解决方法。
第一步,从网上搜索到可以恢复到最近提交的代码

192: huehn$ git reflog
d4cc15a (HEAD -> dev) HEAD@{0}: reset: moving to HEAD
d4cc15a (HEAD -> dev) HEAD@{1}: reset: moving to HEAD
d4cc15a (HEAD -> dev) HEAD@{2}: commit: #提交动态代理
a076220 HEAD@{3}: pull origin dev: Merge made by the 'recursive' strategy.

不知道为何它记录了我三次指向head的指令,其实我也就reset过一次而已。但是这个还不是我研究的重点,我需要的是回复代码。因为我被删掉的代码是没有add或者commit过的,所以,git reflog的方式对我来说没什么用途。

第二步,通过stackoverflow找到了一个方法:
https://stackoverflow.com/questions/14251194/how-to-recover-after-i-execute-git-reset-hard-head

If you didn’t already commit your local changes (or at least stage them via git add, they’re gone. git reset --hard is a destructive operation for uncommitted changes.
If you did happen to stage them, but didn’t commit them, try git fsck --lost-found and then search through the contents of .git/lost-found - it will contain all of the objects that aren’t referenced by a known commit, and may include versions of files that were staged.

大意就是,如果你没有add或者commit本地的代码修改,那么git reset --hard就会删掉它。如果你刚好这么干了,并且git reset --hard执行完之后没有进行commit操作的话,你还是有救的:
git fsck --lost-found找出刚才删除的分支里面的提交对象。

192: huehn$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
Checking objects: 100% (296/296), done.
dangling blob 01c052bc80390683996effd8aed92395af4a554e
dangling blob 05405c1fdd7afc0470016ee68261b00c9276872b
dangling blob 0500ee45c1bd6d4ddfe7507adf896b6041e211da
dangling blob 0580409021aecf138b9f8f17889c03c227df76ae
dangling blob 0bc0cb9a19d9a55aecf83d3072f9e2b736bdff28
dangling blob 0fe09f0d8235cf6959318474d77edf04a0c38454
dangling blob 10e0a3cd7bde5e2fbe7a1d3a95f7817a24e9d69d
dangling blob 10209028b058ee887649402d2b9fdb1fc25478a3
dangling blob 1640a4586acff164dd526be8a35252c0b7963676
dangling blob 1a00de2d0bb32017860dc51ca69671f99b938c6a
dangling blob 1dc0437de8a7c1550e8800453bb974b16acf3391
dangling blob 1d40b28a6edc84bae73a367571b53d7e04355d76
dangling blob 1fa0af26c4c02d3f873526fa187ae5c9eba2f21e

后面还有一堆记录暂时不罗列。

然后用git show命令查看一个找到的对象的内容,看看都是些什么东西

192: huehn$ git show 01c052bc80390683996effd8aed92395af4a554e
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#ifndef WTF_OrderMaker_h
#define WTF_OrderMaker_h

#include <wtf/Bag.h>
#include <wtf/HashMap.h>
#include <wtf/Noncopyable.h>
#include <wtf/SentinelLinkedList.h>

namespace WTF {

// This is a collection that is meant to be used for building up lists in a certain order. It's
// not an efficient data structure for storing lists, but if you need to build a list by doing
// operations like insertBefore(existingValue, newValue), then this class is a good intermediate
// helper. Note that the type it operates on must be usable as a HashMap key.
template<typename T>
class OrderMaker {
    WTF_MAKE_NONCOPYABLE(OrderMaker);
:

显然这个不是我的东西,注释里面提到了 Licensed to the Apache Software Foundation,namepace还是WTF,估计是某些class文件。
用show找了好几个,都不是我的,后面还有一堆这样的blob,显然这个方法有点耗时。

然后
$ find .git/objects -type f | xargs ls -lt | sed 20q
看了看近20记录。

192: huehn$ find .git/objects -type f | xargs ls -lt | sed 20q
-r--r--r--  1 huehn  staff       83  6 27 23:11 .git/objects/5c/42105eb7d14d858c99c03d4b10337d22321906
-r--r--r--  1 huehn  staff      190  6 27 23:02 .git/objects/d4/cc15a05f78781dff4f6ea46790f503c25dc7b9
-r--r--r--  1 huehn  staff      470  6 27 23:02 .git/objects/af/128249c99524d2dad1394b81b6f2ea21157e12
-r--r--r--  1 huehn  staff      221  6 27 23:02 .git/objects/bf/dfe9f281cd27806515d161dc28c22131600e26
-r--r--r--  1 huehn  staff      109  6 27 23:02 .git/objects/5f/adc0eda4c519ede7d68a802791a71ec2b1c646
-r--r--r--  1 huehn  staff      153  6 27 23:02 .git/objects/77/91e8f76499909dc3a0feede62d6cc34da3b5d3
-r--r--r--  1 huehn  staff       46  6 27 23:02 .git/objects/83/9ada8cb2ff59b2ae9965b557b83b998d91a5a4
-r--r--r--  1 huehn  staff       47  6 27 23:02 .git/objects/20/184f36a392ec9b11fd9848f536da6f55d09ebe
-r--r--r--  1 huehn  staff       51  6 27 23:02 .git/objects/79/16c0f9d0865f5cbd8f8fc88a2218e8434e318d
-r--r--r--  1 huehn  staff      305  6 27 23:02 .git/objects/65/c3d8dfcdb4bd51b1817184c88957eb583ce2f6
-r--r--r--  1 huehn  staff       74  6 27 23:02 .git/objects/77/132217c58a28482d406b96c34cc95926b3597a
-r--r--r--  1 huehn  staff      194  6 27 23:02 .git/objects/68/4ca81b6eac47954ad4302cccce160c09c338a8
-r--r--r--  1 huehn  staff      207  6 27 23:02 .git/objects/7b/520a35106722bec71ac26f11346f630296a002
-r--r--r--  1 huehn  staff      250  6 27 23:02 .git/objects/6f/4e86b1b81c54fa0b97cf9a5ca9cab5665c58c1
-r--r--r--  1 huehn  staff      695  6 27 23:01 .git/objects/57/d158e4603d27c7ca4853bd3af0daab7637e032
-r--r--r--  1 huehn  staff      227  6 27 23:01 .git/objects/a0/76220f5f6bb842b4253faff83fea7223433012
-r--r--r--  1 huehn  staff      469  6 27 23:01 .git/objects/7d/c6af00b6c3134ebfbe69785f4764664a859f20
-r--r--r--  1 huehn  staff      221  6 27 23:01 .git/objects/04/55e12cdb90ae4241e11f1ae6bcffafe1407a69
-r--r--r--  1 huehn  staff      108  6 27 23:01 .git/objects/c8/dcf6eec400cd525685470fdddfbac1575af60f
-r--r--r--  1 huehn  staff      153  6 27 23:01 .git/objects/1c/5355fc9992f843842c5b79f520fb74439345db

然后用git cat-file -p 看看里面是什么东西,-p 后面的文件名是倒数两个路径的结合

192: huehn$ git cat-file -p 42105eb7d14d858c99c03d4b10337d22321906
fatal: Not a valid object name 42105eb7d14d858c99c03d4b10337d22321906
192: huehn$ git cat-file -p .git/objects/5c/42105eb7d14d858c99c03d4b10337d22321906
fatal: Not a valid object name .git/objects/5c/42105eb7d14d858c99c03d4b10337d22321906
192: huehn$ git cat-file -p 5c42105eb7d14d858c99c03d4b10337d22321906
package com.huehn.initword.service;

public class ProxyHandler {
}
192:HWBApp huehn$ 

显然这是我的其中一个文件

package com.huehn.initword.service;

public class ProxyHandler {
}

很好。
至于为什么在reset之后还能在.git中找回这些代码。后续我会继续研究一下。估计如果我reset之后直接commit的话,就什么都没了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值