Android stuido项目中有些文件是不需要提交到Git管理的。比如
local.properties
,xxx.iml
等文件都是每个开发者独有的一些配置。JDKB版本,SDK位置都各不一样。每个人都提交很明显会有冲突问题,最好的方式就是不加入git管理
1.在项目根目录创建.gitignore文件
该文件用与编写需要过滤的文件
2.编写gitignore文件
常规用法
-
.gradle
-
/
local
.properties
-
/
.idea
-
.DS_Store
-
/build
-
*
.iml
-
/captures
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3.更详细的用法
在github用一个专门为各个平台提供的gitignore的写法
传送门:https://github.com/github/gitignore
Android.gitignore内容预览
-
# Built application files
-
*
.apk
-
*
.ap_
-
-
# Files for the ART/Dalvik VM
-
*
.dex
-
-
# Java class files
-
*
.class
-
-
# Generated files
-
bin/
-
gen/
-
out/
-
-
# Gradle files
-
.gradle/
-
build/
-
-
# Local configuration file (sdk path, etc)
-
local
.properties
-
-
# Proguard folder generated by Eclipse
-
proguard/
-
-
# Log Files
-
*
.log
-
-
# Android Studio Navigation editor temp files
-
.navigation/
-
-
# Android Studio captures folder
-
captures/
-
-
# Intellij
-
*
.iml
-
.idea/workspace
.xml
-
.idea/libraries
-
-
# Keystore files
-
*
.jks
- 1
- 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
也就是GitHub官方推荐的.gitignore文件书写格式:
Android.gitignore
当然我们可以根据需求继续添加,例如在# Intellij下继续添加:
*.iws.idea/
- 1
相关过滤规则举例说明:
#:注释符号,自动被Git忽略
*.iml:过滤所有的.iml后缀的文件
.gradle/:过滤掉.gradle文件夹
local.properties:过滤掉local.properties文件
Git文件忽略正是通过编写.gitignore文件实现的。之后通过.gitignore忽略的文件则不会被提交到GitHub。
对比下本地AS的工程目录和提交到GitHub上的工程目录:
local.PNG
github.PNG
无论是通过SVN还是Git管理项目,建议在项目初创建时就做好文件忽略的工作,再提交到服务器。
最后补充
如果已经将上述文件都加入了git管理,你会发现即使已经添加.gitignore文件后,git仍然会将上述文件加入管理。此时你需要先删除这些文件(可先备份好),在提交给git仓库。在将刚才删除文件取回即可。