21

I have a iOS project that I have some changes haven't commit. I clicked discard all changes mistakenly. I actually want to only discard a specific file's changes. But I clicked wrong button. And now everything are gone. How to undo the changes?

git status shows nothing.

yong ho
  • 3,892
  • 9
  • 40
  • 81

4 Answers4

148

You might not be out of luck. Although Git can't help you, Finder may be able to come to the rescue!

If you have not turned off the Versions feature (which by default is turned on and you need a Terminal command to disable), then do the following:

  1. Quit Xcode.
  2. Open the file you wish to bring back your lost changes to using TextEdit.
  3. Go to File > Revert To > Browse All Versions...
  4. Scroll through the available versions. Hopefully you will have many available to choose from.
  5. Select a version and hit Restore.
  6. Follow 2-5 for all necessary files.
  7. 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.

starball
  • 20,030
  • 7
  • 43
  • 238
George Marmaridis
  • 1,814
  • 1
  • 13
  • 15
  • Hey there, I am new to git and posted my question [here][1] Basically i have done the same thing, but I was in the process of trying to setup a git workflow on a project I have spent 6 months working on. I found the project in the trash with the files and code, but after I restored to the desktop the project does not display anything. So how can i restore correctly from the trash ? [1]: http://stackoverflow.com/questions/39297971/restoring-project-after-discarded-all-changes-to-xcode-project?noredirect=1#comment65931886_39297971 – A.Roe Sep 02 '16 at 18:53
  • I created a CLI for your solution! https://github.com/cprovatas/git-recover It uses the `NSFileVersion` API that TextEdit uses to more easily recover your changes – Charlton Provatas May 28 '18 at 23:33
  • what if there is nothing in your textEdit Revert to? please help – gkolman Aug 22 '19 at 06:14
5

If you clicked on "discard changes" before committing any changes, your project will become blank and you will find all files in your trash. There is not an whole xcode project there, but you will find all the files which have dates of when you created them. I solved my issue by getting the file I needed from trash. Then I put it back into my project.

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
Iya
  • 1,888
  • 19
  • 12
1

I think you are out of luck. See this answer: Accidentally reverted to master, lost uncommitted changes

Since you did not commit, or stage any of your files, then having reverted them means they are lost.

Sorry.

Community
  • 1
  • 1
BHendricks
  • 4,423
  • 6
  • 32
  • 59
0

I restored it by using Timeline (it's a built-in app in Mac).

give it a try,

Go to Timeline -> locate and select the project file in finder -> Click restore.

that's it.