0

I have regularly committed my progress and wanted to push it today. For some reasons I got an error having a deattached head and (deny updating a hidden ref). Now, after doing various steps in order to reattach it my work is gone. I'm unable to recover it or even find these commits. I committed frequently but didn't push. Now I'm somewhat lost on what to do.

Here's the history, I thought I'm on the branch Feature_SaveAs_Unfinished.

 2004  git commit -am "Removing old logs"
 2005  git push
 2006  git pull origin Feature_SaveAs_Unfinished 
 2007  git push origin origin/Feature_SaveAs_Unfinished 

None of these worked due to the deattached head.

 2007  git push origin origin/Feature_SaveAs_Unfinished 
 2008  git push --mirror
 2009  git pull
 2010  git pull origin Feature_SaveAs_Unfinished 
 2011  git push origin Feature_SaveAs_Unfinished 
 2012  git status
 2013  git checkout origin/Feature_SaveAs_Unfinished

Here I'm getting a message, if I wanted to save my work I should create an extra branch for it.

 2014  git branch Feature_SaveAs_040221_Automerge
 2015  git checkout origin/Feature_SaveAs_Unfinished 
 2016  git status
 2017  git pull
 2018  git pull origin Feature_SaveAs_Unfinished 
 2019  git checkout origin/Feature_SaveAs_Unfinished 

At this point I looked up some solutions on SO: https://stackoverflow.com/a/5772882/2516892 The next steps are according to that post:

 2021  git branch temp
 2022  git checkout temp
 2023  git log --graph --decorate --pretty=oneline --abbrev-commit master origin/master temp
 2024  git diff master temp
 2025  git diff origin/master temp
 2026  git branch -f master temp
 2027  git checkout master
 2028  git status
 2029  git branch -d temp

And I'm trying to get back to the branch I was working at as I'm not on master.

 2030  git status
 2031  git branch -f Feature_SaveAs_040221_Automerge master
 2032  git checkout Feature_SaveAs_040221_Automerge 
 2033  git status
 2034  git pull

At this point I can't find my work any more. The files I've changed are some very old version. (probably from master). I don't know how to get back to them.

I know I have recently used the in a debug-message, so I checked for it.

$ rgrep -rni ☆
Binary file .git/objects/98/9574bda27235aa00dd6ad2846512c69a87a2a3 matches
Binary file .git/objects/9d/c18eb8bba4d381ad0a96c782fec57928dc92d2 matches
Binary file .git/objects/pack/pack-ca6560ce8bd83b69093cea6fbd492ff9ef1a54c0.pack matches

This one is something else:

$ ll 9574bda27235aa00dd6ad2846512c69a87a2a3 
-r--r--r-- 1 qohelet qohelet 2244035 Nov  3 17:38 9574bda27235aa00dd6ad2846512c69a87a2a3

But that's it:

$ ll c18eb8bba4d381ad0a96c782fec57928dc92d2
-r--r--r-- 1 qohelet qohelet 3581541 Feb  2 16:51 c18eb8bba4d381ad0a96c782fec57928dc92d2

I have used some tools like gitk to show the last commits. I can't find any. Even the currently created branch Feature_SaveAs_040221_Automerge points to commits done weeks ago (even though I created the branch half an hour ago).

What to do? How would I obtain my changes and commits to a deattached commit?

EDIT (1):

$ git reflog 
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{0}: checkout: moving from Feature_SaveAs_040221_Automerge to Feature_SaveAs_Unfinished
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{1}: reset: moving to HEAD
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{2}: checkout: moving from Feature_SaveAs_Unfinished to Feature_SaveAs_040221_Automerge
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{3}: checkout: moving from Feature_SaveAs_040221_Automerge to Feature_SaveAs_Unfinished
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{4}: checkout: moving from master to Feature_SaveAs_040221_Automerge
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{5}: checkout: moving from Feature_SaveAs_040221_Automerge to master
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{6}: checkout: moving from Feature_SaveAs_Unfinished to Feature_SaveAs_040221_Automerge
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{7}: pull: Fast-forward
5ac1604 HEAD@{8}: checkout: moving from Feature_SaveAs_040221_Automerge to Feature_SaveAs_Unfinished
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{9}: checkout: moving from Feature_SaveAs_040221_Automerge to Feature_SaveAs_040221_Automerge
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{10}: checkout: moving from master to Feature_SaveAs_040221_Automerge
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{11}: checkout: moving from temp to master
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{12}: checkout: moving from b21832d0d8e5b9b58aa33cd8fd1c11815f635756 to temp
b21832d (HEAD -> Feature_SaveAs_Unfinished, origin/Feature_SaveAs_Unfinished, master, Feature_SaveAs_040221_Automerge) HEAD@{13}: checkout: moving from bb442dfc6900031ab9a17be4f3ea465a90d7b86f to origin/Feature_SaveAs_Unfinished
bb442df HEAD@{14}: commit: Removing old logs
f7ef5de HEAD@{15}: commit: SaveAs sollte gehen. Test fehlt noch

Git show

git show bb442df
commit bb442dfc6900031ab9a17be4f3ea465a90d7b86f
Author: qohelet
Date:   Thu Feb 4 09:03:16 2021 +0000

    Removing old logs

diff --git a/DjangoMoP/settings.py b/DjangoMoP/settings.py
index 3e81225..316d976 100755
Qohelet
  • 1,459
  • 4
  • 24
  • 41

1 Answers1

1

Run git reflog to get Git to spill out the contents of the HEAD reflog. This will get you commit hash IDs (on the left), numbered @{...} suffixed names (next to the hash IDs), and information about what made that the commit that HEAD identified (for commits, that's commit subject lines).

If one of the subject lines looks promising, try that commit hash ID: e.g., git show hash. If that's the commit you're looking for, you've found it: make a branch name for it. If not, keep going with the reflogs. To make a new branch name newname that points to a given commit hash hash, use git branch newname hash.

Given that you have some data you can search for (or a blob hash ID like 9dc18eb8bba4d381ad0a96c782fec57928dc92d2—see the output of git show 9dc18eb8bba4d381ad0a96c782fec57928dc92d2 to see if that's a file of interest, for instance) you can also search the commits in the reflog to see if any of them have, in their snapshots, a file with that blob hash ID. That's usually less effective than the quick "look at the top few reflog entries" method, but note that git ls-tree -r hash | grep 9dc18eb8bba4d381ad0a96c782fec57928dc92d2 is a way to find out whether the given commit hash ID hash refers to that version of the file.

Side note: the answer you linked referred to someone who was in the middle of a git rebase when they did stuff on their detached-HEAD setup, and then lost it; the commands you quoted are useful after you've set up a branch name to be able to find the commits.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thank you! Indeed, it does look good! I added `git reflog` and `git show bb442df` in my initial post. I did `git branch Feature_SaveAs_040221_Resurrect bb442df`. You saved my life. Thank you – Qohelet Feb 04 '21 at 10:59