2

I'm doing some work with Xcode, using local git first. In order to share the code, I started trying GitHub. Somehow, I made a mess.

Today, after the code was working to some extent, I actives GitHub to upload the code. Then I found that I have nothing locally left.

I had all my code in User/Developer and 3 nights work is gone :(

Anyway that I can restore my code?

user427969
  • 3,836
  • 6
  • 50
  • 75
RAX
  • 211
  • 1
  • 12
  • 2
    Did you check your git reflog? (as in http://stackoverflow.com/a/21778/6309 ) – VonC Jul 11 '12 at 07:23
  • Hi, Thanks for your reply! I haven't tried that yet. Right now, if I go to Xcode, I can no longer see in repository all my history. It now shows the one synched from GitHub master branch(which has nothing there). And I've lost all my code :( Following instructions in the link you pointed just now, will it get both my files back and the history? – RAX Jul 11 '12 at 07:37
  • If you have committed locally, then you can restore that old SHA1. But any non-committed work is likely lost (unless you have the time machine - like in http://www.macworld.com/article/1164963/mac_basics_how_to_set_up_time_machine.html - activated on that drive) – VonC Jul 11 '12 at 07:56
  • Hi, I don't have time machine set up. But I did committed all my local changes before I was using GitHub. I'lll google a bit where/how git stores initial revisions and changes, and how to restore them. Thanks, VonC! – RAX Jul 11 '12 at 16:27
  • ok I have put together an answer highlighting the two common cases where data can "disappear" and how to get the right commit back in the working tree. – VonC Jul 11 '12 at 17:18
  • XCode uses AutoSave but doesn't offer a menu item to revert to previous versions. If you open the files in TextEdit, you can do File > Revert and even see the whole AutoSave history. – qingu Nov 28 '14 at 16:13

1 Answers1

1

If you have, as you comment, commit your changes before seeing them gone when activating GitHub link within XCode, then you should be able to restore those commits:

git reflog is the way to look at all commits, including the one no longer referenced.
See for instance "The latest commit gone after hard reset".

git branch -a can also show you the current branch and help you see if you need to checkout your first branch back. This kind of scenario happens when a remote branch (still empty) is accidentally checked out: see "can git checkout be undone?".
If that was the case (ie, if XCode mistakenly checked out a remote empty branch), you can easily go back to the branch you were with:

git checkout -
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you so much! I'll try it immediately when I get back home tonight. – RAX Jul 11 '12 at 21:17
  • Thank you so much! I see all my history. but when I tried "git checkout -" it showed: Ruian:RpnCalculator Ruian$ git checkout - .gitignore: needs merge error: you need to resolve your current index first – RAX Jul 12 '12 at 08:21
  • okay... I think I did the most unprofessional thing I ever did. Just tired "$ git reset --hard HEAD" and seems I really lose everything now :( My bad... – RAX Jul 12 '12 at 08:49
  • @RAX so no commit before that reset? Because if there were commits, they are in the reflog. – VonC Jul 12 '12 at 08:58
  • yes... I saw the commits before I blindly did "git reset-hard HEAD". Now there's no history :( sorry, my bad... – RAX Jul 13 '12 at 16:41
  • @RAX but that's my point: if there *were* commits, a `git reset --hard` won't delete them (simply hide them in the reflog) – VonC Jul 13 '12 at 20:20
  • Hi, VonC, sorry that I'm replying late. I was on my way for a business trip and just arrived yesterday in China. I'll be able to check and update later tonight. Thanks for your help! – RAX Jul 17 '12 at 00:05