Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seemed to be in “(no branch)” and then lost my changes

Tags:

git

I was trying to push from my Git workspace up to Github but my added and committed changes didn't seem to get uploaded.

Then, doing a "git branch" I got something that looked like this :

git branch
* (no branch)
  master

Foolishly, I thought I could get back into master with

git checkout master

and now my changes seem to have gone. My master branch is about three days old. And there seems no way to switch back to this (no branch).

I checked this question Git : seemed to be in “(no branch)” and then lost my changes where answers suggest to do a git reflog showfollowed by a checkout. I tried that and I got this:

$ git reflog
    0f27ae7 HEAD@{1}: checkout: moving from HEAD to master
    7b8ee7b HEAD@{2}: commit: 14/05/2017 3:33pm
    ff60409 HEAD@{3}: commit: 14/05/2017 3:33pm
    0f27ae7 HEAD@{4}: checkout: moving from master to 0f27ae7236aabbe8cccfba82e201e36368a05054
    0f27ae7 HEAD@{5}: commit: 11/05/2017 2:33pm
    3e4c616 HEAD@{6}: merge origin/master: Fast-forward
    1e79818 HEAD@{7}: commit: 10/5//2017 UI

I tried to do a checkout from 0f27ae7236aabbe8cccfba82e201e36368a05054 but my changes weren't back. What I want is to restore the last commit I made in (no branch) (commit: 14/05/2017 3:33pm).

Here is the result of git branch -a:

$ git branch -a
* (HEAD detached from 0f27ae7)
  UI_linking
  master
  remotes/ado/newBranch
  remotes/origin/UI_linking
  remotes/origin/frogs1
  remotes/origin/master
  remotes/origin/newBranch
  remotes/origin/newMas

Are my changes lost? Or is there a way to recover them?

like image 609
AbdelKh Avatar asked Dec 18 '22 07:12

AbdelKh


1 Answers

From the output of git reflog I would say your changes are on commit 7b8ee7b (HEAD@{2}).

The following commands:

git branch lost 7b8ee7b
git checkout lost

should create a new branch (named lost) on the aforementioned commit then check it out.

Then you can do:

git rebase master
git checkout master
git merge --ff lost

to move the two commits you recovered on top of the master branch and make them appear as part of the master branch history.

If everything looks good then you can run git branch -d lost to remove the lost branch.

like image 174
axiac Avatar answered Dec 20 '22 22:12

axiac