17

I'm working on an Objective C project in Xcode and need to restore to a previous version that was committed in source control. How do I do that? I can find information on how to commit and push but no information on how to restore to an old version.

Victor Engel
  • 2,037
  • 2
  • 25
  • 46
  • What source control system what interface? – zaph Jan 12 '13 at 22:18
  • 1
    I'm using Xcode's built-in git support. I occasionally push to github, but I also have local commits. If this didn't answer your question it's because I don't know enough about how this works. – Victor Engel Jan 12 '13 at 22:27
  • I use a 3rd party app: Tower for those times I find Xcode Git support lacking. [Tower](http://www.git-tower.com) - 30 day free trial. – zaph Jan 12 '13 at 22:29
  • 1
    Oh, good grief! I was pulling my hair out trying to figure out why code wasn't working in the iPhone simulator. My purpose for starting this thread was to restore to a known point that worked to try to backtrack. That didn't resolve the issue either. It turned out the problem was with the simulator somehow. After quitting the simulator and running my project again, my issues disappeared. Lesson learned: if something doesn't work in the simulator, nuke it and try again. – Victor Engel Jan 12 '13 at 23:09
  • Considering that you may have learned the most basic of basics of source code control, that was well worth it. Imagine you are told that a user found a bug which was not present in the version that you published a year ago. And someone introduced that bug at some point in the last year. And your job is to find which change introduced the bug. – gnasher729 Apr 06 '14 at 13:19
  • 1
    If you want you can take a look to this article as well ["Revert to the desired commit (Xcode)"](http://iosdevblog.com/2014/04/05/revert-to-the-desired-commit-xcode/) Hope it helps... – angelos.p Apr 06 '14 at 11:05

7 Answers7

30

You cannot revert the entire project at once in Xcode, but you could do it from the terminal; just cd into the folder of your project and type git log to find the hash you're looking for, then do git checkout [hash].

spongessuck
  • 1,023
  • 8
  • 23
mlstudent
  • 948
  • 2
  • 15
  • 30
  • After doing this, where do I find the restored version? Sorry, I'm still learning the ways of git – Ethan Fischer Jan 14 '16 at 13:42
  • 1
    @EthanFischer in Git, "checkout" moves you to a different place in the version history, or a different branch. You're still working locally and in the same directory when using "checkout." You're not making new copies of any files or pulling anything from a server. This can be confusing especially since in Xcode (7.3) the term "Checkout" is used in the "Source Control" menu to refer to what, in Git, is "Clone" which in fact does pull down files that exist on a server and places them in a local directory. – Daniel Jonce Evans Mar 29 '16 at 15:15
  • Hi, tried this and got the following , message: error: Your local changes to the following files would be overwritten by checkout: Drop Down Menu 3.5/DropDown Menu Storyboard/DropDown Menu Storyboard-Prefix.pch Please, commit your changes or stash them before you can switch branches. Aborting – dancingbush Jan 13 '17 at 20:20
  • I changed the accepted answer to this one because 1) The link in the previously accepted answer no longer works,and 2) Having used git for a few years now, I know how it works. What I was previously looking for was at odds with how git works. I'd say the correct answer should be mu. Restoring is not what you want to do in a git environment. Branching from an older commit is what you want. – Victor Engel Sep 26 '17 at 02:38
13

Quote from Apple's documentation at https://developer.apple.com/library/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/Save_and_Revert_Changes_to_Files/ManageChanges.html

"Compare File Versions to Revert Lines of Code Choose View > Version Editor > Show Comparison View to compare versions of files saved in a repository. Use the jump bars to choose file versions based on their position within a repository. Each jump bar controls the selection for the content pane above it. To display a version, browse through the hierarchy to find it, then click to choose it. Shaded areas indicate changes between versions." "You can use the version timeline to choose file versions based on their chronological order. Click the timeline viewer icon (../art/TimeLineIcon_2x.png) in the center column to display the timeline between the two editing panes. Move the pointer up or down through the timeline to browse the available versions. When you find the version you want, click the left or right indicator triangle to display that version in the corresponding editor pane. You can edit the current working copy of the file in the version editor. If you want to revert changes between versions, you can copy code from an older version and paste it into the current version."

The last line reveals the key: copy-and-paste.

Hackless
  • 385
  • 5
  • 14
  • I no longer have a need for this, but since I believe it answers my question, I am checking it. Thanks. – Victor Engel Nov 27 '13 at 02:44
  • Thank you for responding to this otherwise cold trail. I was struggling with this versioning issue myself and found your question as the most on-topic. -Xin – Hackless Nov 28 '13 at 06:27
  • 3
    Wow. Apple should definitely make this more intuitive. If Xcode can save your code somewhere, you kinda expect to get it back, right? Or is that just me? Great work Apple. I'm gonna submit a feature request. – erdekhayser Jan 05 '14 at 04:54
  • 3
    I don't think this is a solution because you can't really do this to all files in a reasonable way. What about the storyboard and other files without code. I think it would be smarter to say: No you can't do that, terminal is the only place where you can access all git commands, but you can go back to individual files and copy and paste changes. – 3366784 Aug 20 '15 at 22:01
  • This fetches only a certain number of versions that can fit in the page. If we have too many commits on that file, how can we see it? @Hackless – KarenAnne Jan 28 '16 at 12:13
  • @KarenAnne If XCode does not provide adequate git integration, we just have to go back to command line usage of git, as the answers by mlstudent and 3366784 say. – Hackless Feb 05 '16 at 04:33
  • 1
    @Hackless the link doesn't work anymore... but thanks for getting the info :) – bearacuda13 Dec 31 '16 at 04:06
11

In Xcode 9 you can checkout any previous version.

  1. Click on the source control icon in the navigator section on the left, or go to view->navigators->Show source control navigator.
  2. A list of all commits for the selected branch will be shown.
  3. Right click on the desired commit and select 'Checkout "commitID"'

You can also directly create a branch from there which might be even more useful in some cases

Andy
  • 866
  • 9
  • 14
4

here is a visual method in Xcode 8

enter image description here

MiladiuM
  • 1,449
  • 16
  • 17
  • does discard all changes kill your project? – bearacuda13 Dec 31 '16 at 04:17
  • @bearacuda13 no it reverts it to the previous commit. If that is your first commit then yes it becomes a new project – MiladiuM Dec 31 '16 at 06:31
  • This just discards all the changes you have made since the last commit and deletes them for good. You can not jump back to an arbitrary commit in the history like this. – Andy Sep 22 '17 at 16:19
2

If you want to get an entire project extract of any committed version on a separate folder, try this shell script:

#!/bin/sh
#
#  restores an old version of xcodeproject from git repository
#  into folder  $HOME/gitcopy
#  current directory must be the rootdir of the project
#  Xcode should not access the same project at this time
#
set -x
PROJECT_ROOT=.
COPY_DEST=$HOME/gitcopy
if [ -e *.xcodeproj ]
then
    git log
    echo "Which version ?"
    read x
    git checkout $x
    git checkout -b copyVersion
    cp -R $PROJECT_ROOT $COPY_DEST
    git stash
    git checkout master;
else
    echo "No xcode project root folder";
fi 
1

I am working on Xcode 9.2 project which has several dependencies and additional file that have been incorporated into it. In the course of cleaning it up, I inadvertently trashed the project and could not figure out what to do to get it to run until I found this solution. I did go to source control on the top menu bar and discarded all changes first. Then I went to the Source Control Manager (second from the left just to the right of the Project Navigator and highlighted the commit I wanted then right clicked and chose the Checkout option which had already entered the number of the commit I wanted and it worked perfectly. Step 1 - Discard all changes Step 2 - Select Commit you want to go back to. Step 3 - Right click and select Checkout

0

I'm late but I had this issue and luckily for me, my previous version was under "stashed changes".

On the left where the file negation is, choose the second option, you should see a folder named stashed changes, under it will show all the available previous versions. Just right-click on it and click "apply stashed changes"

showcase image

Dharman
  • 30,962
  • 25
  • 85
  • 135
Fire assassin
  • 81
  • 1
  • 9