Git : seemed to be in "(no branch)" and then lost my changes

Solution 1:

As long as you've not done a git gc, then you've not lost anything. All you need to do is find it again :) What do you get with:

git reflog show

That should show you what happened, and the id of the missing node(s).

Solution 2:

The above answer is correct. This is what I did:

$ git reflog
5b35f6d HEAD@{1}: pull github master: Fast forward
ca92d15 HEAD@{2}: checkout: moving from 759dab1b15731ce7680c26839ca470d20e709e36 to master
759dab1 HEAD@{3}: commit (merge): Merge branch 'master' of github.com:gonzojive/IODB-ui into HEAD
065e269 HEAD@{4}: commit: added fieldsets to snazzy form
f357606 HEAD@{5}: commit: preliminary support for google maps.
ca92d15 HEAD@{6}: checkout: moving from master to ca92d15d272867b63d54f96d4aa57f8ecc479cd0

$ git checkout ca92d15d272867b63d54f96d4aa57f8ecc479cd0

The "Oh No!" moment is this:

checkout: moving from master to ca92d15d272867b63d54f96d4aa57f8ecc479cd0

ca92d15d272867b63d54f96d4aa57f8ecc479cd0 is the anonymous branch that shows up as (no branch). To get back to it, just do a git checkout and your old pseudobranch is restored.

I recommend backing up your git repository before you accidentally gc it, just for peace of mind.

Solution 3:

# if you have already checked out to master, 
# you won't know the commit-ish of your "no branch":

git fsck --lost-found # (to find your <commit-ish>)
git merge <commit-ish>

# if you are still on your "no branch" commit:

git log # (the commit-ish will be on the first line)
git checkout master
git merge <commit-ish>

# or

git log | head -n 1 | cut -d ' ' -f 2 | pbcopy
git checkout master
git merge <commit-ish>