Save and Revert Changes

Xcode automatically saves changes to source, project, and workspace files as you work. This feature requires no configuration, because Xcode continuously tracks your changes and saves them in memory. Xcode then writes these changes to disk whenever you:

You can also manually save changes to disk by choosing File > Save.

You’ll occasionally want to revert a file to a previous state. For example, you might experiment with a new user interface layout and then decide to revert to the previous layout. Or you might need to undo some code changes because they introduced a problem. The Revert Document command in the File menu and the Undo command in the Edit menu let you discard changes to a file.

You might want to revert an entire project to a previous state. For example, after performing a global search-and-replace operation across multiple files in a project, you might decide to return all the files to their previous state. You can restore all your files and settings from a project snapshot.

You should also use source control management to keep track of changes at a fine-grained level. A source code management system saves multiple versions of each file onto disk, storing historical metadata about each version of each file in a source code repository. You can use the Xcode version editor to compare changes between versions of a file. You can copy portions from earlier versions of the file and paste them into the current version. With a source control system, you can also branch from a stable version of your project, add new features and make other changes to the branch, and then merge and reconcile those changes back into your project.

Revert to the Last Saved Version of a File

To discard all changes you’ve made to a file since it was last saved to disk, choose File > Revert Document. The Revert Document command operates only on the file that has the editing focus. You can give editing focus to a file either by clicking its editor pane or by selecting it in the project navigator.

The Revert Document command always returns the contents of the file to the last saved version on disk. If you would prefer to back out changes one change at a time, use the Undo command in the Edit menu.

Undo File Changes Incrementally

To back out changes to a file incrementally, choose Edit > Undo change. The Undo command is contextualized by your last operation. For example, the command appears as Undo Typing if you make an edit to an implementation file; the command changes to Undo Add Button if you add a button object to a storyboard.

With the Undo command, you can back out every change to a file since the start of your editing session. An editing session begins when you open a project and ends when you close the project. Xcode lets you undo all the edits in that session, even those already saved to disk. (Note, however, that the Revert Document command clears the Undo history, and you cannot undo a revert operation.)

After you’ve chosen the Undo command, you can choose Edit > Redo to reverse the last undo operation.

Use Snapshots to Restore Projectwide Changes

The Revert Document and Undo commands are not supported for three types of projectwide changes:

Therefore, use a snapshot to revert changes across a project.

Snapshots are archives that include all document files in the project and all project and workspace settings. Snapshots provide an easy way to back up the current version of your project or workspace. If something goes wrong because of a code change you make, you can restore your entire project, even a deleted project, to a previous state.

Xcode automatically creates a project or workspace snapshot before you perform any mass-editing operation, such as refactoring your code or executing a Find and Replace operation. You can also set Xcode to automatically create snapshots in other circumstances, such as before a build, by choosing Xcode > Preferences, selecting Behaviors, and selecting the Create Snapshot option. You can also create a snapshot manually by choosing File > Create Snapshot.

To see the snapshots for a project or workspace, choose Window > Organizer, select Projects to open the projects organizer, and click the project.

../art/select_snapshot_2x.png

You can recover an earlier state of a project or workspace by exporting a snapshot from the projects organizer. Select a project or workspace, select a snapshot, and click the Export Snapshot button at the bottom of the window. Xcode displays a preview dialog in which you can review the differences between the snapshot version of a file on the left and the current version on the right.

../art/review_export_changes_2x.png

Click Export, select a folder to to export your snapshot to, and click Export again.

You can also restore a snapshot on top of the current project by choosing File > Restore Snapshot from the project’s workspace window. Xcode displays a preview dialog in which you can review the differences between the current version of the project and the snapshot version. When you click Restore, Xcode replaces the current version of the project with the version in the snapshot. Xcode takes a snapshot of the current version before replacing it.

You can restore a deleted project from a snapshot because Xcode keeps track of all your projects, even deleted ones, and displays them in the projects organizer.

Store and Track Changes with Source Control

Use commands in the Source Control menu to manage your project files with a source code repository. A repository saves multiple versions of each file onto disk, storing historical metadata about each version of each file. Source control allows you to keep track of file changes at a finer level of detail than snapshots allow. Source control also helps you coordinate efforts if you work with a team of programmers.

A source control system helps you reconstruct past versions of a project. You can commit a file to your repository each time you make a major change. Then, if you find you’ve introduced bugs, you can compare the new version of the file with a past version that worked correctly to help to locate the source of the trouble.

When multiple people work on a project, source control helps prevent conflicts and helps resolve conflicts should they arise. By keeping a central repository that holds the master copy of the software, the source control system allows each programmer to work on a local copy without danger of corrupting the master. With a file checkout system, you can ensure that two people don’t work on the same code at the same time. If two people do change the same code, the system helps you merge the two versions.

Xcode supports two popular source control systems: Git and Subversion. Subversion (often abbreviated svn) is always server based. The server is normally on a remote computer (although it is possible to install the server locally). Git can be used purely as a local repository, or you can install a Git server on a remote computer to share a repository among team members.

If you are working alone, it’s easiest to use Git, because you won’t need to set up a server. When you create a project, Xcode automatically sets up a Git repository for you.

../art/CreateGitRepo_2x.png

In addition to performing continuous integrations, the Xcode service, available with OS X Server, hosts Git repositories.

If your repository is on a server, choose Source Control > Check Out to create a local working copy of the project on your computer. If you use a local Git repository, you don’t check out a working copy, because your local repository is your master copy.

When you are satisfied with changes you've made to a file, choose Source Control > Commit to ensure that those changes are preserved in the repository. You are required to provide a comment explaining the nature of your commit. If your Git repository is on a server, the commit operation adds your changes to your local repository. Perform a push operation to add your committed changes to the Git repository on the server. For example, when you choose Source Control > Commit on your development Mac, select the “Push to remote” option, specify the remote repository in the pop-up menu, and click Commit Files.

You can see the source control status of your files in the project navigator. The status is shown as a badge next to the filename.

../art/LocalModifiedFiles_2x.png
Badge
SCM status

M

Locally modified

U

Updated in repository

A

Locally added

D

Locally deleted

I

Ignored

R

Replaced in the repository

The contents of the folder have mixed status; display the contents to see individual status

?

Not under source control

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.

../art/VersionEditorWithChanges_2x.png

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.

Create a Branch to Isolate Risky Changes

After you’ve worked on a project for a while, you are likely to have a body of reliable, stable code. You can choose Source Control > Working Copy > New Branch to create a copy of that code. Then you can work on new features and other changes without destabilizing your existing code base. When you are satisfied with your changes, you can merge them back into the body of stable code. Use Source Control > Working Copy > Merge from Branch and Source Control > Working Copy > Merge into Branch to combine and reconcile differences between versions of your project.