看板 KnucklesNote
作者 標題 [Xcode][Swift3] 新增 .gitignore 略過不需加入 git 的檔案
時間 2017-03-22 Wed. 14:07:23
專案目錄會有一些系統檔案或是編譯時的暫存檔
不想把這些檔案加到 Git 記錄,
也不想每次 Commit 時都看到這些檔案
![[圖]](http://i.imgur.com/CZt2ojI.png)
新增一個 .gitignore 檔
在 Xcode 雙擊專案後選「New File...」
![[圖]](http://i.imgur.com/4tGH0OJ.png)
選最下方的「Empty」後點「Next」
![[圖]](http://i.imgur.com/7Ov0t6U.png)
檔名輸入「.gitignore」後
Where 要選儲存專案的資料夾,點「Create」
![[圖]](http://i.imgur.com/U0fqzoX.png)
出現警告訊息,開頭為"."的檔案是給系統做隱藏檔用的
點「Use "."」確定要用這個檔名
![[圖]](http://i.imgur.com/hMLvs82.png)
在 Finder 按 command+shift+. 顯示隱藏檔
確認一下 .gitignore 跟 .git 資料夾在同個目錄
![[圖]](http://i.imgur.com/NE3cz3T.png)
輸入 .gitignore 內容
.gitignore 的內容可以使用 https://www.gitignore.io/ 來產生
![[圖]](http://i.imgur.com/kvla9I2.png)
例如輸入「osx xcode swift」後,點「Create」
![[圖]](http://i.imgur.com/r4WsFXy.png)
產生的內容為 https://www.gitignore.io/api/osx,xcode,swift
# Created by https://www.gitignore.io/api/osx,xcode,swift
### OSX ###
*.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Swift ###
# 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
## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM
## Playgrounds
timeline.xctimeline
playground.xcworkspace
# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
.build/
# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts
Carthage/Build
# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
### Xcode ###
build
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
# End of https://www.gitignore.io/api/osx,xcode,swift
### OSX ###
*.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Swift ###
# 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
## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM
## Playgrounds
timeline.xctimeline
playground.xcworkspace
# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
.build/
# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts
Carthage/Build
# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
### Xcode ###
build
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
# End of https://www.gitignore.io/api/osx,xcode,swift
執行 Commit 將 .gitignore 檔加進記錄
![[圖]](http://i.imgur.com/nxkRiyp.png)
此時那些要略過的檔就不會再出現了
自訂一個不要被加入 .git 的 .plist 檔
例如要使用一些網站的 API 時
必需將自己申請到的 API Keys 隱藏起來才行
點專案裡面的資料夾,
新增一個 Property List 檔,名稱輸入「keys」
![[圖]](http://i.imgur.com/GSKrike.png)
注意檔案不能存在專案根目錄,要存在放程式的目錄裡
在 .gitignore 裡加入 keys.plist
![[圖]](http://i.imgur.com/91lzGqq.png)
在 keys.plist 檔裡輸入 API Keys
![[圖]](http://i.imgur.com/04TPx5G.png)
要讀取 API Keys 時,使用
var keys: NSDictionary?
if let path = Bundle.main.path(forResource: "keys", ofType: "plist") {
keys = NSDictionary(contentsOfFile: path)
}
let myApiKey = keys?["myApiKey"] as? String ?? "{myApiKey}"
print("myApiKey: \(myApiKey)")
可以在 {myApiKey} 填入預設值,if let path = Bundle.main.path(forResource: "keys", ofType: "plist") {
keys = NSDictionary(contentsOfFile: path)
}
let myApiKey = keys?["myApiKey"] as? String ?? "{myApiKey}"
print("myApiKey: \(myApiKey)")
當 keys.plist 不存在的時候就會使用預設值
之後 Commit 時,就不會把 keys.plist 加進去了
參考
StackOverflow Git ignore file for Xcode projects
StackOverflow How to hide API keys in GitHub for iOS (SWIFT) projects?
--
※ 作者: Knuckles 時間: 2017-03-22 14:07:23
※ 編輯: Knuckles 時間: 2017-05-10 10:28:04
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 2280
回列表(←)
分享