12

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?

interstar
  • 1,281
  • 4
  • 18
  • 23

3 Answers3

14

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

CK.
  • 1,163
  • 6
  • 10
  • 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 02: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! – Falgantil Sep 25 '15 at 14:19
6

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.

  • 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 `. The commit id is the alpha numeric code in the first column of `git reflog`. See http://effectif.com/git/recovering-lost-git-commits. – Torsten Sep 30 '15 at 13:22
2
# 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>
  • 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