Ignoring Files That Are Already in a Git Repository

March 5th, 2018

Filed under: Version Control, Xcode | Be the first to comment!

In a previous post, I showed how to tell git to ignore files in Xcode projects. But what do you do if you want to ignore a file that git is currently tracking?

Remove the file from the git repository. Add the file you want to ignore to the git ignore file. If you don’t perform this step, git will track the file when you add it back to the repository. Add the file back to the project after removing the file from the repository.

Removing a File That’s in an Xcode Project from the Repository

To remove a file that’s part of an Xcode project from a git repository, you must remove the file from the project. Select the file from the project navigator on the left side of the project window and press the Delete key.

Xcode will ask you if you want to remove the reference to the file from the project or move the file to the Trash. Move the file to the Trash.

After removing the file from the project, choose Source Control > Commit to commit and remove the file from the repository.

XcodeCommitRemovingFile

Add the File back to the Project

Now that you removed the file from the repository, get the file out of the Trash and move it back to your project folder. Choose File > Add Files to ProjectName to add the file back to your Xcode project.

Learn More About Version Control

Facebooktwittergoogle_plusredditmail

Telling Git to Ignore Files in Xcode Projects

February 28th, 2018

Filed under: Version Control, Xcode | Be the first to comment!

When you create a project in Xcode and create a git repository, git initially tracks every file in the project, but this behavior isn’t what you want. Xcode projects contain a lot of files. Some of these files shouldn’t be tracked by git because they change every time you build your project. Having files you don’t want in git being marked as updated and ready to commit is annoying at best. At worst Xcode won’t let you switch branches because of uncommitted changes.

How do you get git to track the files you’re interested in tracking, such as your source code files, and not track the files you don’t care about? Create a git ignore file and add it to your git repository.

Generating a Git Ignore File

The git ignore file is just a text file with the name .gitignore. It contains a list of files and directories. Git will ignore any of the files or directories it finds in the git ignore file.

What files and directories do you put in a git ignore file? That depends on the tools and languages you use to build your software. Fortunately the website gitignore.io has done the hard work for you. Go to gitignore.io and enter a programming language or an IDE to create a git ignore file. If you go to the site and enter Xcode, gitignore.io creates the following git ignore file:

# Created by https://www.gitignore.io/api/xcode

### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, 
#   Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno

# End of https://www.gitignore.io/api/xcode

Xcode project files have a lot of files in them, don’t they?

Saving the Git Ignore File

Now you need to copy the git ignore file into a text file. Open a text editor (not a word processor like Pages or Word). If you don’t have a text editor, download BBEdit or Visual Studio Code. They should do the job.

Copy the contents of the git ignore file into the text document. Save it in the project folder with the name .gitignore. Files that start with . are invisible in the Finder. To show hidden files in the Finder, hold down the Command and Shift keys and press the period (.) key. If you navigate to your project folder, you should see the git ignore file. Press Cmd-Shift-Period to go back to hiding the invisible files.

Adding the Git Ignore File to Your Repository

After creating the git ignore file, add it to your repository. In Xcode choose Source Control > Commit. A sheet opens.

XcodeCommitGitIgnoreHighlighted

Select the checkbox next to the git ignore file and deselect any other checkboxes. Add a commit message and click the Commit button.

Learn More About Version Control

Facebooktwittergoogle_plusredditmail

Xcode 9: Adding Your Project to GitHub

February 21st, 2018

Filed under: Version Control, Xcode | Be the first to comment!

Before Xcode 9, setting up your project to work on GitHub involved a lot of work. You had to login to GitHub, create a repository, go back to your Mac, launch the Terminal application, navigate to your project directory, and run multiple git commands.

Xcode 9 makes working with GitHub much easier. You can add your Xcode project to GitHub without leaving Xcode.

Start by going to the source control navigator by choosing View > Navigators > Show Source Control Navigator.

SourceCodeNavigatorAtStart

Select the Remotes folder, right-click, and choose Create Remote on GitHub. A sheet similar to the following will open:

CreateGitHubRemoteBlur

Unless you’ve already configured a GitHub account in Xcode, choose Add a GitHub Account from the Account menu.

GitHubLogin

Enter your GitHub account name and password. Click the Sign In button to use your GitHub account for this repository.

Choose public visibility unless you have a paid GitHub account. You must have a paid account to create private GitHub repositories.

Click the Create button to create the remote branch. Your Xcode project is now on GitHub.

Learn More About Version Control

Facebooktwittergoogle_plusredditmail

Handling Swipe Gestures in a SpriteKit Game

February 16th, 2018

Filed under: Game Development, iOS Development | Be the first to comment!

When you create a SpriteKit Xcode project, the GameScene.swift file contains functions to handle touch and/or mouse events, depending on the type of SpriteKit project you create. You can also use swipe gestures in SpriteKit games. Supporting swipe gestures requires you to perform two tasks.

The first task is to create a swipe gesture recognizer. The swipe gesture recognizer takes two arguments: a target and a selector (function) to call when someone performs the gesture. Set the swipe gesture recognizer’s direction and add it to the view. The following code demonstrates how to create a swipe gesture recognizer for swiping right:

override func didMove(to view: SKView) {
    let swipeRight = UISwipeGestureRecognizer(target: self, 
        action: #selector(GameScene.swipeRight(sender:)))
    swipeRight.direction = .right
    view.addGestureRecognizer(swipeRight)
}

The second task is to write a function to handle the swipe.

@objc func swipeRight(sender: UISwipeGestureRecognizer) {
    // Handle the swipe
}

The functions to handle the swipes need the @objc at the start because they’re selectors that access the Objective-C runtime.

Facebooktwittergoogle_plusredditmail

Creating a Git Repository for an Existing Xcode Project

February 12th, 2018

Filed under: Version Control, Xcode | Be the first to comment!

When you create a new Xcode project, Xcode gives you the option to create a git repository on your Mac.

CreateGitRepoNewProject

How do you create a git repository for a project after the project has been created?

Choose Source Control > Create Git Repositories. A sheet opens.

Xcode9AddGitRepoExistingProject

Click the Create button and Xcode will create a git repository for your project.

Learn More About Version Control

Facebooktwittergoogle_plusredditmail