Xcode has had Source Control support via
git 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:
.ipa 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.
gitignore 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
But what files did Xcode generate that you want to ignore? The good news is there is a website, gitignore.io, which collects
gitignore 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. [Note: 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
.gitignore in the top-level of your Xcode project.
gitignore 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
rm command, but not readded by the
add. Be sure you are running this in the top-level directory of a clean repo (no modified or uncommitted files).
git rm -r --cached . git add . git commit -m"Add .gitignore and remove existing gitignored files from repo"
If as time goes on, you encounter additional files that cause the same problem, add those problematic patterns to your
gitignore, and redo the git commands above. It’s a simple, no hassle way to get to a clean, minimal git repo.
Did you like this tip? The next tip on why you should never use force unwrapping in Swift code is already waiting for you. Or sign up to get every tip straight to your inbox.
This post originally published at Apps Dissected.