Przemek
Przemek

Reputation: 69

Recover lost data GIT detached head

Im not very good at GIT (I dont really undestarnd this HEAD and "master" things) and I've just lost my code. Can anyone help me to retrieve it?

My steps were like:

  1. I added some code and I wanted to commit it
  2. A window popped up with a message like "Warning: HEAD is detached", but I clicked "commit anyway"
  3. I switchced to a previous commit as I wanted to look for something in the old code

Now, I can not go back to the first step. I tried a few things that I found on stackoverflow, but I cannot get my original code (from the step 1.) back. Is it lost forever or is there a way to retrieve it?

I was using a PHPStorm graphical interface for GIT, but it looks like I can see all my steps in the console, so I`m gonna paste it below, so you can see what exactly my steps were:

08:19:51.448: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -- js/common/VuexStoreCategories.js
08:36:06.443: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false reset -- components/vue/summary/SummarySum.vue.php
08:36:06.585: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false rm --ignore-unmatch --cached -r -- <list_of_some_files_with_my_code>
08:36:06.694: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- <list_of_some_files_with_my_code>

<warnings about replacing LF with CRLF>

08:36:08.397: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false add --ignore-errors -A -f -- <list_of_some_files_with_my_code>

<warnings about replacing LF with CRLF>

08:36:08.835: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false commit -F C:\Users\Przemek\AppData\Local\Temp\git-commit-msg-.txt --
[detached HEAD 030b7b5] w połowie rozbijania vuex summary na moduły
 286 files changed, 11322 insertions(+), 5531 deletions(-)
 create mode 100644 AJAX/logout.php
 <list of info about files changes like "create", "rewrite", "rename" ... >

08:36:50.494: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout a28074265e45de506ab69098cfa97ceb4908e957 --
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
  030b7b5 w połowie rozbijania vuex summary na moduły
If you want to keep it by creating a new branch, this may be a good time
to do so with:
 git branch <new-branch-name> 030b7b5
HEAD is now at a280742 Update 08.09.2021
08:38:24.842: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout master --
Previous HEAD position was a280742 Update 08.09.2021
Switched to branch 'master'
08:40:00.813: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false checkout 41ef9415a73ff920c89d1caccde2768521da9a34 --
Note: switching to '41ef9415a73ff920c89d1caccde2768521da9a34'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
  git switch -c <new-branch-name>
Or undo this operation with:
  git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 41ef941 w połowie przenoszenia summary do vue
09:05:53.832: [src] git -c credential.helper= -c core.quotepath=false -c log.showSignature=false cherry-pick 41ef9415a73ff920c89d1caccde2768521da9a34
Auto-merging classes/TestClass1.php
Auto-merging classes/TestClass2.php
On branch alt-history
You are currently cherry-picking commit 41ef941.
Untracked files:
The previous cherry-pick is now empty, possibly due to conflict resolution.
    AJAX/hello world.xlsx
If you wish to commit it anyway, use:
    AJAX/temp.php
    TO_LOAD/
    git commit --allow-empty
    exporters/
Otherwise, please use 'git cherry-pick --skip'
    <list of graphic files excluded from git>
nothing added to commit but untracked files present

Upvotes: 1

Views: 163

Answers (1)

Przemek
Przemek

Reputation: 69

Problem solved. As Romain Valeri mentioned in the comment I had to do the following steps:

  1. "git branch [new-branch-name] 030b7b5" - to create a new brach with my lost files (this 030b7b5 comes from my logs)
  2. "git branch [new-branch-name]" to switch to this new branch
  3. Click "reload files from disk" in PHPStorm

Thanks again Romain Valeri for solving the problem ;)

Upvotes: 1

Related Questions