Home > Blockchain >  GIT - lost everything after being accidentally in detached HEAD and making a wrong commit?
GIT - lost everything after being accidentally in detached HEAD and making a wrong commit?

Time:01-15

I dont remember what I did but all my work from months is gone. For long time I was committing files to git. I must have misspelled the commit command, I dont remember (and cannot see it anymore). Then the following happend:

I was in detached HEAD

I might have stashed files

I checked out to main branch I found my commit but in this branch and last commit almost everything is gone.

In my history in detached head

commit 2be5fe718cf6ed42250b7b190573b04300697b50 (HEAD, main)
Reflog: HEAD@{0} (...)
Reflog message: checkout: moving from main to 2be5fe718cf6ed42250b7b190573b04300697b50
Author: ...
Date:   Sat Jan 14 18:14:59 2023  0100

     change getcloseststation to first select a line

commit 2be5fe718cf6ed42250b7b190573b04300697b50 (HEAD, main)
Reflog: HEAD@{1} (...)
Reflog message: commit: change getcloseststation to first select a line
Author: ...
Date:   Sat Jan 14 18:14:59 2023  0100

     change getcloseststation to first select a line

commit a2f2d7c0f126e954606249bfc728cc930841dce1 (origin/main)
Reflog: HEAD@{2} (...)
Reflog message: checkout: moving from 804021e233b9edcbb68e08dae1a901d6fb23d3cc to main
Author: ...
Date:   Sat Sep 17 16:57:40 2022  0200

I tried to go there and do git stash pop (since I think I stashed it) but nothing there to be stashed. I only have 2 branches which is main and detached HEAD.

Also in both detached HEAD and main, I can only see my last commit and another comment 4 months ago. All my other commits disappeared - BOTH in detached HEAD and main.. Im afraid to do anything more wrong and fully loose everything. I know its not fully clear what happened, but its all I know. I would appreciate help so much as its soo much Ive lost..Thanks a lot!!!

when running git log 2be5fe718 or gitk 2be5fe718 on copied repo, I only get the following (1 commit from4 months ago and my last commit that I did today but where everything is gone

commit 2be5fe718cf6ed42250b7b190573b04300697b50 (HEAD -> main)
Author: ...>
Date:   Sat Jan 14 18:14:59 2023  0100

     change getcloseststation to first select a line

commit a2f2d7c0f126e954606249bfc728cc930841dce1 (origin/main)
Author: ...>
Date:   Sat Sep 17 16:57:40 2022  0200

    checkout

EDIT:

2be5fe71 (HEAD -> main) HEAD@{9}: commit: change getcloseststation to first select a line
a2f2d7c0 (origin/main) HEAD@{10}: checkout: moving from 804021e233b9edcbb68e08dae1a901d6fb23d3cc to main
804021e2 HEAD@{11}: commit: change getcloseststation to first select a line

CodePudding user response:

  1. Create a backup of your directory in case your screw up.

  2. Use git reflog to find the lost commit(s). You can use git log $commit_hash or gitk $commit_hash to verify.

  3. Once you have the commit hash, create a new branch to point to that commit: git branch $commit_hash_from_reflog

  4. Check out or push this new branch

  5. Don't forget to be happy

  •  Tags:  
  • git
  • Related