Source code version control is a core competency of the modern developer. But sometimes the system and the environment can get in the way.
When using Xcode (or other tools) with services such as GitHub the default situation is to associate a folder with a new repository, including all files and subfolders contained within. This might also include some files or folders you might not want to include.
For example, macOS uses hidden files named
.DS_Store (Apple’s Desktop Services Store) to manage information about the state of local folders, and Xcode places files in
xcuserdata/ folders which track the state its own UI.
Both of these are useful for the local user, but are unlikely to be so for others who might be working on your project code.
Before Your First Project
If you want to prevent all of your local projects including selected files or folders in their repositories, then you can update Git’s global settings.
Step One: create a .gitignore file
Create a file named
.gitignore_global in your local user root directory. For example, by using the following command in the Terminal:
_global suffix to highlight to future you that this affects all repositories.
Step Two: add the required rules to the .gitignore file
Using your preferred text editor, add the name of each file or folder that you want to be excluded from all of your Git repositories. For the examples above, the file would contain (with #-prefixed comments included):
# macOS .DS_Store # Xcode xcuserdata/
There are other options, such as using wildcards, which can be explored as extended reading.
Step Three: Configure Git to use the .gitignore file
Use the Terminal to tell Git to use this list of files folders to be ignored when creating all new local repositories:
git config --global core.excludesfile ~/.gitignore_global
Ignoring by Project
You might want to ignore files on a project by project basis.
The process is the same, but in each project root folder you should include a
.gitignore file, formatted as above.
Fixing Files that Slip Through the Net
If you had already linked your Xcode project via Git, and after setting up your global, or per-project, ignore files, perhaps now have the odd erroneous file such as
.DS_Store in one of your repositories, you can remove the individual files from your repository:
git rm --cached .DS_Store
This will need done for each rogue file in your local repository, with files being removed from your remote repository on the next