iOS项目的.gitignore配置
# 前言
iOS 项目多人协作开发的时候,一言不合就有冲突。冷静分析,这里面有坑,有老梗,也有一些是可以规避的,.gitignore就是一个避免冲突的利器。
# .gitignore
在我们使用 git 的过程当中,不是任何文件都需要 commit 到本地或者远程仓库的,比如 Mac 的老梗 .DS_Store,看得是真的烦。一些第三方框架类库文件,很大,动不动就是几百M,还有用户个人元数据,包括断点信息等等,都没有必要提交。
# .DS_Store
Mac 下的老梗 .DS_Store 文件我之前已经解决了,可以参考 《解决讨厌的.DS_Store文件(上)》 和 《解决讨厌的.DS_Store文件(下)》。
虽然这个文件可以清除,但还是添加上去,反正也不亏。
# Pods
以往我们都是 Pod install 后就无脑把 Pods 全部提交到本地,然后推远程仓库,导致远程仓库项目非常大,clone 花了很长的时间,每次 pod update 后 Push 代码也非常久。后来考虑了一下,觉得 Pods 这个目录可以不提交,甚至 Podfile.lock 这个文件也可以不要,继而 .xcworkspace 也可以不要,只要留一个 Podfile 文件,并在里面指定每个依赖库的版本号就够了,只要团队的每个人都有 CocoaPods 环境,每个人 pod install 后,在本地就能跑起来了。
# xcuserdata
在项目根目录下,一般有 .xcodeproj 和 .xcworkspace文件,显示包内容可以看到都有 xcuserdata 文件夹,然后里面放着 username.xcuserdata文件夹,再点进去就是 UserInterfaceState.xcuserstate 和 Breakpoints_v2.xcbkptlist,这些都属于用户个人设置,像断点记录这样的文件肯定不能提交上去合并的,没有意义,也容易导致冲突。
# 其它
另外看情况将不需要的文件名,文件后缀,或者目录都可以添加到 .gitignore 中。比如做持续构建的时候,如果 Build 目录在项目中,可以将 Build 目录 ignore 掉,也可以将 *.ipa 忽略掉,空文件夹不会被跟踪,效果一样。具体可以根据自身项目需要进行修改。
# 结合以上几点,简便配置
# Xcode
.DS_Store
build
report.xml
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.moved-aside
DerivedData
.idea/
*.hmap
*.xccheckout
*.xcworkspace
!default.xcworkspace
#CocoaPods
Pods
Podfile.lock
xcschemes
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# gitignore.io 选择自定义配置
在 gitignore.io 输入需要配置的语言,会自动生成一份配置。比如,输入 Objective-C 和 Swift 会帮助你生成下面的配置。
# Created by https://www.gitignore.io/api/swift,objective-c
### Objective-C ###
# 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
# 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
# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode
iOSInjectionProject/
### Objective-C Patch ###
### Swift ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
## Build generated
## Various settings
## Other
## Obj-C/Swift specific
## 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
# 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
# End of https://www.gitignore.io/api/swift,objective-c
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
这个配置自动生成了很多注释和一些不太必要的配置,所以直接使用上面提供的简便配置即可。
# 取消跟踪
一般来说,git init 后就应该把 .gitignore 也配置好,而不是等到项目开发到一半才发现觉累不爱。当然,如果真有这种情况,也有解决办法。
假设需要忽略掉的是在桌面的 Project 文件夹下的 Pods 目录,那么需要下面这三步。
# 添加 .gitignore
$ { echo 'Pods'; } > ~/Desktop/Project/.gitignore
# 删除 git 追踪文件
$ git rm –cached ~/Desktop/Project/Pods
# Commit && Push
$ git commit -a -m "Bye Bye Pods."
$ git push
2
第二步还有另外一种方法来删除追踪,就是先把 Pods 移到别的目录,然后再移回来。因为重新移回来的时候 .gitignore 就生效了,就忽略它不会再跟踪了,效果跟 git rm –cached 是一样的。
# 总结
.gitignore 可以忽略没必要提交的文件和目录,极大地减轻冲突几率,也可以让远程仓库更小一些。项目一开始就配置好 .gitignore,只留一个 Podfile 即可。如果项目进行到一半,添加完 .gitignore 后,需要删除追踪文件并重新提交。