Take the 2-minute tour ×
Server Fault is a question and answer site for professional 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)
  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 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
add comment

3 Answers

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
2  
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
add comment

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.

share|improve this answer
add comment
# 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
add comment

Your Answer

 
discard

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.