2

I have been working on an add-on for this big commercial product (that ships source code). This add-on I am developing is located within the folder: ~/dev/project/addons/my_addon

To keep things maintainable I created a git repo in this folder. I worked and saved the most import things with a git commit.

Yesterday evening I was missing a feature within the core product, this I thought I could build this myself and send them the patch. This I did:

cd ~/dev/project
git init
git add *
git commit -m 'init'

I thought this would be nice, since if I would accidentally make the program crash I could just git reset --hard and for the creation of the patch file I could just git patch.

Yesterday I worked for 6 hours to include this patch and in the meanwhile I did some coding in my own add-on. This morning I made a big error. I wanted to reset the changes I made within the project folder. But I did this within my add-on, I've lost over 30 hours of work.

Within ~/dev/project I have the following git history:

stolas@dev-laptop:Commercial_License_3.73% git log
commit 40f4059dd05311997b5093077e69e89535b1ecb6
Author: Stolas <stolas@my_domain.org>
Date:   Fri Apr 25 23:31:57 2014 +0200

    init
stolas@dev-laptop:Commercial_License_3.73% git reflog 
40f4059 HEAD@{0}: checkout: moving from master to 40f4059
40f4059 HEAD@{1}: reset: moving to 40f4059
2f1ce07 HEAD@{2}: commit: stash, I want my **** files back
40f4059 HEAD@{3}: commit (initial): init
stolas@dev-laptop:Commercial_License_3.73% git status
HEAD detached at 40f4059
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)

    modified:   addons/my_addon (new commits, modified content, untracked content)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .history
    Sessions/25042014.log
    Sessions/25042014--23_36_16.log
    Sessions/26042014--00_17_28.log

no changes added to commit (use "git add" and/or "git commit -a")
stolas@dev-laptop:Commercial_License_3.73% 

Where within my ~/dev/project/addons/my_addon I have got the following git history:

stolas@dev-laptop:my_addon% git log  
commit eff5291b50ad09970c6386ef4deaa70f6e8a0270
Author: Stolas <stolas@my_domain.org>
Date:   Sat Apr 26 08:27:34 2014 +0200

    Revert "Revert "First setup nearly done.""

    This reverts commit 6567b65523898e625b8ded8b058885ca51f2aae4.

commit 6567b65523898e625b8ded8b058885ca51f2aae4
Author: Stolas <stolas@my_domain.org>
Date:   Sat Apr 26 08:27:14 2014 +0200

    Revert "First setup nearly done."

    This reverts commit b3c55d3f736f15c78f71ada5f0bf5ae46cf8dd71.

commit b3c55d3f736f15c78f71ada5f0bf5ae46cf8dd71
Author: Stolas <stolas@my_domain.org>
Date:   Wed Apr 23 22:48:39 2014 +0200

    First setup nearly done.

commit b8c234a4cf52e89367d49788594442d8142624d8
Author: Stolas <stolas@my_domain.org>
Date:   Sat Apr 19 09:01:36 2014 +0200

    Start Env

commit cc4d0b7add2ed852043e549716481207ec7ef13b
Author: Stolas <stolas@my_domain.org>
Date:   Sat Apr 19 09:00:24 2014 +0200

    Start Envoirment button

commit 92a2ac263b58adf8265d8edb4e14c075ee7b0ad1
Author: Stolas <stolas@my_domain.org>
Date:   Sat Apr 19 08:58:13 2014 +0200

    Setup
stolas@dev-laptop:my_addon% git reflog 
eff5291 HEAD@{0}: reset: moving to HEAD^
11bce53 HEAD@{1}: commit: dialog
eff5291 HEAD@{2}: revert: Revert "Revert "First setup nearly done.""
6567b65 HEAD@{3}: revert: Revert "First setup nearly done."
b3c55d3 HEAD@{4}: commit: First setup nearly done.
b8c234a HEAD@{5}: commit: Start Env
cc4d0b7 HEAD@{6}: commit: Start Envoirment button
92a2ac2 HEAD@{7}: commit (initial): Setup
stolas@dev-laptop:my_addon% git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   libs/my_addon_main.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .idea/
    libs/my_addon_paint.py
    libs/my_addon_msging.py
    template/dialog.xml

no changes added to commit (use "git add" and/or "git commit -a")

I thought I could fix this by making a checkout on the lower git for the init commit, but it seems to reset everything except my git folder. How can I reset a git within a git to the yesterday? Or revert my revert(s)

The file I desperately want back is libs/my_addon_main.py as most of the work is within this file.

gturri
  • 13,807
  • 9
  • 40
  • 57
Stolas
  • 279
  • 1
  • 3
  • 19

1 Answers1

3

Recovering from a git reset --hard depends on several factors (to be examined from within your nested git repo, where you did a reset --hard by mistake):

  • your changes (now lost) were already committed:
    You can see see with a git reflog, and recover from there

  • your changes were added to the index, but never committed.
    You can still find them, using this answer, based on git fsck --unreachable --no-reflogs --no-cache HEAD.

  • you changes weren't committed not added:
    Your IDE could provide its own local history of the file (as in this answer)

  • If everything else fails, checks backups (like TimeMachine on Mac) or disk recovery tool (like Recuva mentioned in this answer)

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Everything failed, I feel I might just need to take the hit. And take this as a reminder to never ever use GIT when you are sleepy and make shadow copies of all file edits every hour.. – Stolas Apr 27 '14 at 09:21
  • @Stolas yes, or make bundle regularly, as in http://stackoverflow.com/a/1251717/6309 (it generates only *one* file, easier to manage than a folder deep copy) – VonC Apr 27 '14 at 09:22
  • Did it fail because you hadn't staged your changes, or was there another problem? – user3426575 Apr 28 '14 at 23:47
  • @user3426575 There was another problem. However yesterday evening I got bored and started rewriting most of it. Thus I'll just give the bounty. – Stolas Apr 29 '14 at 08:39