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

Tags:


Leave a Reply

Your email address will not be published. Required fields are marked *