Android studio项目忽略某些文件提交Git

Android stuido项目中有些文件是不需要提交到Git管理的。比如local.properties,xxx.iml等文件都是每个开发者独有的一些配置。JDKB版本,SDK位置都各不一样。每个人都提交很明显会有冲突问题,最好的方式就是不加入git管理

1.在项目根目录创建.gitignore文件
该文件用与编写需要过滤的文件

2.编写gitignore文件
常规用法


   
   
  1. .gradle
  2. / local .properties
  3. / .idea
  4. .DS_Store
  5. /build
  6. * .iml
  7. /captures
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.更详细的用法
在github用一个专门为各个平台提供的gitignore的写法
传送门:https://github.com/github/gitignore
Android.gitignore内容预览


   
   
  1. # Built application files
  2. * .apk
  3. * .ap_
  4. # Files for the ART/Dalvik VM
  5. * .dex
  6. # Java class files
  7. * .class
  8. # Generated files
  9. bin/
  10. gen/
  11. out/
  12. # Gradle files
  13. .gradle/
  14. build/
  15. # Local configuration file (sdk path, etc)
  16. local .properties
  17. # Proguard folder generated by Eclipse
  18. proguard/
  19. # Log Files
  20. * .log
  21. # Android Studio Navigation editor temp files
  22. .navigation/
  23. # Android Studio captures folder
  24. captures/
  25. # Intellij
  26. * .iml
  27. .idea/workspace .xml
  28. .idea/libraries
  29. # Keystore files
  30. * .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仓库。在将刚才删除文件取回即可。

猜你喜欢

转载自blog.csdn.net/mawei7510/article/details/82217448