Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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)

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 a day old. And there seems no way to switch back to this (no branch).

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

share|improve this question

3 Answers 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>
share|improve this answer
Out of many ways including rev-list, only fsck helped to find branch-less commit. Thank you very much. – temoto Aug 20 '14 at 12:56

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 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.

share|improve this answer
Simply checking out the branch didn't work for me. My changes were correctly made in my feature branch. Still I couldn't see them in my workspace. I must have lost my changes in some kind of other way. However, I could recover my changes by executing git reset --hard <commit-id>. The commit id is the alpha numeric code in the first column of git reflog. See – Torsten Sep 30 at 13:22

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).

share|improve this answer
I'd do git reflog without specifying the master. It is possible to have commits that aren't tied to a branch, and in this case, that's likely what happened. You can checkout a commit into a new branch so it has a path to it. – Jeff Ferland Aug 20 '09 at 17:32
You're right, probably better that way. My assumption from the question had been that it was on master for the commit. I've updated my answer. – CK. Aug 20 '09 at 21:09
Whew, thanks! I committed a huge stash to no branch, then switched to master to merge it in... and gone my reference was. – Pascal Oct 30 '11 at 2:23
It's been quite a lot of years, but I too jumped into this. I switched back to master branch, and suddenly, poof. All my 2 days of work (that is, 8 hours aday) gone. Thank you so much! – Bjarke Søgaard Sep 25 at 14:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.