Why do these $?@!% files keep changing? Stop the file churn in Xcode with a gitignore file
Tags: ios, xcode, git
Published: 2018-12-03 14:42:24

---
title: Why do these $?@!% files keep changing? Stop the file churn in Xcode with a gitignore file
published: true
description: 
tags: ios,xcode,git
canonical_url: https://www.appsdissected.com/stop-changing-files-xcode-gitignore/
---
Xcode has had Source Control support via <code>git</code> for a long time. Somehow it’s amazing that even now, in its default configuration, you may find that all sorts of files that show up in your repos that you don’t want: <code>.DS_Store</code>, <code>xcuserstate</code>, <code>build</code>, <code>.ipa</code> files, just for starters.

You want to do the right thing, and you know or have heard that checking user-specific files, temporary files, or build products into source control is not the right thing. You want to avoid merge conflicts that result from files that frequently change. Or you’re just tired of doing something in your IDE, and suddenly your project has modified files to commit.

You should be able to tell Xcode that you want Source Control in your project, and it should handle everything the right way. But Xcode lets you down by not handling this correctly.

What do you do? The good news is that the solution is just a couple of steps away, by applying a gitignore file in your repo.

<hr>

A <code>gitignore</code> file tells git not to add or commit files matching the list of patterns included in the file. For example, this would be an easy one to stop those annoying <code>.DS_Store</code> files:
<pre><code>.DS_Store
</code></pre>
But what files did Xcode generate that you want to ignore? The good news is there is a website, <a href="https://gitignore.io">gitignore.io</a>, which collects <code>gitignore</code> files for many development environments and languages, including Swift, Objective-C, and Xcode. All that’s needed here is to pull up the website, enter Swift into the box and hit Create. [<b>Note:</b> there is an Xcode option as well, but in reviewing it, it looked like it ended up ignoring files that were important. It goes to show that even the Internet isn't perfect, and to always review anything you get from there!]

Save that generated file named <code>.gitignore</code> in the top-level of your Xcode project.

With the <code>gitignore</code> in place, to fix a repo where you’ve already committed these files, you’ll need to remove everything from the repo, and add it all back. This way, any file that is now caught by the .gitignore will be removed by the <code>rm</code> command, but not readded by the <code>add</code>. <strong>Be sure you are running this in the top-level directory of a clean repo (no modified or uncommitted files).</strong>
<pre><code>git rm -r --cached .
git add .
git commit -m"Add .gitignore and remove existing gitignored files from repo"
</code></pre>
If as time goes on, you encounter additional files that cause the same problem, add those problematic patterns to your <code>gitignore</code>, and redo the git commands above. It’s a simple, no hassle way to get to a clean, minimal git repo.

<hr>

Did you like this tip? The next tip on <a href="https://www.appsdissected.com/force-unwrapping-not-swift-like-wrong/">why you should never use force unwrapping in Swift code</a> is already waiting for you. Or <a href="https://www.appsdissected.com/newsletter/">sign up to get every tip straight to your inbox.</a>

This post originally published at <a href="https://www.appsdissected.com/stop-changing-files-xcode-gitignore/">Apps Dissected.</a>