Restoring an Xcode commit that was never pushed to Github - xcode

This is a question that I posted, and then after much digging finally resolved myself. There is actually quite a bit on this subject both on this forum and elsewhere, but it usually requires some familiarity with Terminal. I am going to describe the problem I faced and then describe step-by-step in detail (at a beginner's level) how to resolve the issue in Terminal.
In short, I checked out a previously committed version of my app in Xcode, which - because it was a version from several weeks prior - did not contain my most recent commits. In other words, I had no access to any of the commits that contained my most recent work. They had all disappeared.
My commits and pushes had not stored in GitHub because presumably some time before this I had accidentally selected my main folder as the destination for my commits, rather than one of the two branches I created. So I had absolutely no access to my work. By all appearances I pretty much had to start from scratch.
After much hand wringing, teeth gnashing, and hours of scouring the webs, I finally uncovered the solution. But it takes a bit of understanding about Terminal to make it work. So after several more hours of learning Terminal, I finally successfully restored all of my work.
For any of you who are new to coding (as I am), and who have no experience with Terminal, I will provide detailed instructions on how to resolve this issue if you encounter it in the answer below.

Open Terminal to prompt to your Xcode project. The easiest way to do this is to find your project in Finder, which will have a .xcodeproj extension, and then right click it.
Select New Terminal at Folder.
At this point, a terminal window will pop up. From here, enter the following: git reflog
Press Enter
This will populate a list of all the commits stored in your Xcode project. Each commit is identified by an alphanumeric code (the one I restored was 1a7ea33, for example).
Note the alphanumeric code of the commit you wish to restore.
After this, enter the following: git checkout -b NewBranch 1a1a1a1 (where "NewBranch" is whatever name you decide to name your new branch, and "1a1a1a1" is your alphanumeric code from steps 5 and 6).
Press Enter.
That's it. Close Terminal and open your Xcode project as normal. You will notice the restored commit in the folder you just named in Terminal.
Hopefully no one will ever need this, but if by chance someone does I hope it helps.

Related

Can`t see the file history in Gitlens (on a specific project)

I have a problem with Gitlens extension. Firstly, Gitlens or Git History extension is working fine on any other project except the this Laravel project. My problem is, can`t see the file history or line history in this project.
On the other hand, there is no problem in the git log (Git History Extension) as seen in screenshot;
But I can't compare any files with their any version (every commit seems like in the ss). I have no idea what the problem is about.
Lastly, I don't think the problem is about extension settings because other projects do not have this problem. Is there a any chance to fix this issue without resetting git history?
I am answering after 3 months, maybe it works for someone. As I mentioned in the title, surely there is no problem about extension settings for this specific project. The problem is just about git files. Maybe, I damaged some git files while moving the project on my desktop :)
So, there is no chance to fix this issue without resetting git history or starting fresh with git, if you`re having the same problem.
Do not play with git files, it`s no joke!
I've got a similar issue. My problem is in using symbolic links for folders.
I have this folder symlink for my repos.
C:\>dir
Directory of C:\
27.09.2018 21:07 <JUNCTION> repos [C:\Users\Qwerty\repos]
(GitLens v11)
The top editor is opened via C:\repos
The bottom editor via C:\Users\Qwerty\repos
You can see that some git features still work (yellow), but others don't.
However, this is very strange, because in my other repos, I have never observed such issues.
Also notice below that it is still possible to invoke the same action via a git lens popup and then it is possible to use the Prev Next Revision buttons.
Anyway, after more testing, I actually observe the same thing in my other repos. I assume that something must have changed between versions as I only observe these issues for last maybe 2-3 months. I even submitted a ticket that GitLens stopped working in v12.0.0 and later versions https://github.com/gitkraken/vscode-gitlens/issues/1979
The buttons completely disappeared and even the commit information
v11
v12
And Now actually, after having reopened my repo via original path and after updating GitLens back to v12.0.6 I see that it is working properly!
Bottom editor is opened via C:\repos symlink.

Source file is blank and Git doesn't show it ever existed

I was coding earlier and decided to not commit. When I revisited the project later in the day, I went to Source Code/Discard All Changes. I didn't like the direction I was taking the project, so I figured I would discard changes and then reclone my project into another folder to get back to my original state. I've been working on this project for a few weeks now, and this one particular file has been part of the project since the beginning.
It's showing up in my project tree on the left side of XCode, but it's empty. When I look in the folder, it's straight up missing. No problem, I'll restore from github. This is where the problem is. It doesn't show up in any of my commits. I don't see this file ever existed, but if I look at some of my commits, I see references to the file. Is there any way for me to recover this file, or do I need to start coding on it again? This really sucks... :(
I've done 'git log --full-history -- path/to/file' and it doesn't find anything at all. I do the same for a known good file, and it shows the file. This file isn't in my history at all. I thought that was the whole point of git was to be able to go back in time if something happened?

Problems Using Snapshots in Xcode 5 Mac

A couple of people have asked a similar question, the last one a year ago, with no solution (Here and here), so I thought I'd try it again.
I've created snapshots in Xcode 5.1 on a few occasions before making significant code changes. When I go to restore them, I've seen a couple of different behaviors:
There's one file that changed. Xcode shows me the before and after diffs, and I say OK, and click on Restore. The result, nothing's changed.
Same as one, except in this case there are multiple files that have changed. It shows me one, and it looks good. So I click Restore. It doesn't show me any of the other files diffs that have changed, and it doesn't prompt me to be sure I don't want to look at them. After the fact, I've surmised that I need to click on each changed files in order to accept the changes. But still, there's only one Restore button, which dismissed the window. In any case, none of the files have been restored to their former state. If I try to restore from that same snapshot again (naively thinking I could try to retrieve the other changed files), it tells me that the current state and the snapshot are the same.
If I export the changes to another directory, then I do get the reverted files. It's when I do a File->Restore Snapshot where I get no results.
I've taken to manually creating zip archives of the source so I can recover the files manually.
I don't have any Source Control in use in Xcode so far. It seems I should do that if only to do periodic commits on my own.
Do other people use snapshots successfully? Has anyone else experienced this behavior in Xcode 5?
Thanks much for your help. I've yet to get any response on the Apple Developers Forum on the topic :(
-Eric

XCode - Any way to recover code after "Discard Changes"?

I inadvertantly did a very stupid thing last night. I have been working on an iPad project for a couple of months and (even more stupidly) do not have a backup. By right-clicking on the project in XCODE, I mistakenly clicked "Discard Changes"
(stupidity reigns again) I did not have any commits since the initial and when I did the Discard Changes, it seems that it went all the way back to the initial commit, which if I am looking at things correctly looks like each file is as it was the very first time it was saved.
My question is: Is there a way to recover my work? Is it in the .git directory? Is a copy stored elsewhere? or as a last resort, any chance to recover some of the source text with an un-delete utility?
Thanks,
Bob
For little clarification.. one can put back files from the trash as discard changes sends the file in the trash
Answer copied from How to undo xcode discard all changes if not committed
Quit Xcode.
Open the file you wish to bring back your lost changes to using TextEdit.
Go to File -> Revert To -> Browse All Versions...
Scroll through the available versions. Hopefully you will have many available to choose from.
Select a version and hit Restore.
Follow 1-5 for all necessary files.
Launch Xcode. You should now see the M next to these files in Project Navigator indicating they include uncommited changes (the changes you previously discarded).
Completely new files you may have created and were discarded can be found in the Trash.
Also a small piece of advice. If you are in need to discard changes, prefer using Source Control -> Discard Changes by right clicking on files in the Project Navigator, instead of Discard All Changes which might give you all kinds of trouble.
Have a nice day. :)
Well luck was on my side after all. As it turns out, when I did the Source Control > Discard Changes operation, it did not make any changes to the source code save in my project directory. I was able to start a new project and added the files back in. Once I did that I was able to compile and run my project in the simulator just as it was before the incident. I now have a backup as well as committed the changes in git.
You have a hope of fetching this code if you "pushed" & committed your prior-to-discarded changes into your local git repository. Or if you're using MacOS's included Time Machine or some other automated backup solution.
Aside from that, you might be hosed.
If you're on Lion or later, you may have Time Machine backups even if you don't have an external drive. As of Lion, Time Machine will take 'local snapshots' of files; this is how they support the "Versions" feature that lets you go back to previous versions of your files.
If you didn't have Time Machine turned on, though... you may be out of luck.

XCode 4.2 Source Control problems

I seem to have a major problem. I opened up Xcode and my main view controller code seems to have been replaced with the original code, it's all gone. All my other files have the little 'M' for modified icon next to them, this problem file however does not. It seems like it's gone back to the original version.
Now, Can I go back to my modified version, or is it all gone? This is a for a test app so although not crucial, I'd really prefer to not lose it all :)
Thanks
Just putting an answer here as I hate to leave things with no answers. Dropbox sorted my problem as I had a backup copy of everything, and I am yet to find out what went wrong. I'm now using a combination of Dropbox and a GIT repo to keeps things save.

Resources