How to add .gitignore file into Xcode project
Asked Answered
M

4

43

When making sure that iPhone project could be installed as an exact copy into a brand new computer via version control (git), I just realized that .gitignore file was missing. Added under version control, but now would like to add that file also into Xcode project for easy viewing and editing.

When using "Control-click + Add Files to myProject..." popup menu, I can't see any filenames starting with . (that's a dot). How can I add my .gitignore file into Xcode project?

Moriyama answered 25/1, 2013 at 7:41 Comment(1)
In my case, there was no .gitignore, instead the rules were specified inside .git/info/exclude (Ref: #5698648)Wavelet
C
90
  1. When the open panel is showing, press Command-Shift-. and the hidden files will appear, including .gitignore.
  2. Select the .gitignore file and press the Add button.

This works in any app, not just Xcode. It's an obscure feature of NSOpenPanel.

Chiccory answered 25/1, 2013 at 7:51 Comment(6)
You rock. Before reading your answer I really thought of adding a random file into project and then editing project file manually to replace that file with the correct one.Charlottcharlotta
Using Xcode 5.0.2 under Mac OS X 10.9.1 and command-shift-dot doesn't show hidden files any more. Any ideas how to fix this?Moriyama
It looks like there's a bug on Mavericks (10.9) that causes the file list to not update immediately. Workaround: press command-shift-., then select something in the Favorites sidebar, then go back to where you were -- then all the files will appear.Chiccory
It may be worth including in the answer that the box to "copy items if needed" should be left unchecked to prevent Xcode from potentially copying the .gitignore into another directory before adding it to your project. Speaking from experience, frustration can result from modifying a .gitignore that doesn't reside where you think it does.Syncrisis
This answer assumes there is already a .gitignore file. There may not be. It should go in the same folder as .git in case you need to create one.Churchless
@VictorEngel is correct, this answer is applicable only if .gitignore file is already present.Classical
H
9

This solution didn't work for me in XCode 8.0 and El Capitan.

I opened a terminal and typed:

defaults write com.apple.finder AppleShowAllFiles YES
cd ../PATH_TO_GIT_ROOT 
touch .gitignore

Then relaunched Finder and my .gitignore file was visible. Edit per .gitignore documentation

Harter answered 4/10, 2016 at 1:36 Comment(0)
H
2

There are only three lines of code that you should write to terminal in order to achieve updated .gitignore file for Xcode 12+.

First step is setting a global configuration for .gitignore file from Gitignore.io

git config --global alias.ignore '!gi() { curl -L -s https://www.gitignore.io/api/$@ ;}; gi'

In the second step go to your project folder then generate the .gitignore file for your project.

git ignore swift,macos >.gitignore 

Lastly, commit the generated file.

git commit -m "Add .gitignore file"

You can open the .gitignore file with Text Editor and uncomment any Folder-File that you don't want to commit into your repo.(For example Pods/)

Howie answered 19/1, 2021 at 16:30 Comment(0)
E
0

Still see this after I had the same problem. And I came here through google to this question here. You can change to your project root folder using a terminal.
Then : touch .gitignore And add whatever you need in the file.

Then in your Mac goto finder | goto your project folder| cmd+shift+. to show hidden files| In Mac OS, drag .gitignore to xcode project root in your xcode. Now it shows up in Xcode!

Etom answered 25/9, 2023 at 4:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.