AbdelKh
AbdelKh

Reputation: 542

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

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?

Upvotes: 4

Views: 1857

Answers (3)

DreamInBox
DreamInBox

Reputation: 184

The reason is: You pushed your branch to no branch that announce some warnings but You ignored them. Now, this code is detached to HEAD.So your new modified code will be on HEAD~. You can see in the relog:

0f27ae7 HEAD@{4}: checkout: moving from master to 0f27ae7236aabbe8cccfba82e201e36368a05054

The way to get it back:

  1. You fetch your git source:

git fetch origin

  1. Checkout to HEAD

git checkout remotes/origin/master

  1. Finally, Merge 0f27ae7 HEAD@{1} to master

git merge HEAD@{1}

Now, You got your code in the master.

Regards,

Upvotes: 0

axiac
axiac

Reputation: 72256

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.

Upvotes: 12

aragaer
aragaer

Reputation: 17858

You can restore your state doing git checkout 7b8ee7b where 7b8ee7b is the hash under HEAD@{2} in your reflog - the one before you switched to master.

What happened: you switched from master branch to 0f27ae7236aabbe8cccfba82e201e36368a05054 for some reason and ended up in "detached head" state. You did some commits but those weren't on any branch. These commits still exist and you can see them in reflog.

While 0f27ae7236aabbe8cccfba82e201e36368a05054 is what your master points to, when you switch to hash, you don't switch to any branch that points to that commit.

Upvotes: 0

Related Questions