Git ignore配置

410 阅读2分钟

使用Git作为版本工具,当有些文件不想使用git进行版本跟踪,怎么办呢,这时需要在工程主目录里增加.gitignore文件。一般IDE自动生成的文件都要加入.gitignore, 下文是一份github上的android工程的.gitignore文件模版,可根据自己的情况进行增删改。

参考官方文档:git-scm.com/docs/gitign…

1.四种添加忽略文件的方法

有四种方式增加忽略规则,优先极从高到低排列如下:

1.命令行

2..gitignore文件

3.$GIT_DIR/info/exclude

4.core.excludesFile

2.根据使用目的选择忽略文件的方法

采用何种方式添加忽略文件取决于你想到达什么样的目的:

1.如果想跟随版本控制且在clone项目时其他开发者也能用,应该使用.gitignore

2.针对特定代码仓库且不与其它仓库共享,应该使用$GIT_DIR/info/exclude

3.对所有git管理的代码仓库都有效,应该使用core.excludesFile在用户的~/.gitconfig文件中。默认值是XDG_CONFIG_HOME/git/ignore如果没有XDG\_CONFIG\_HOME/git/ignore 如果没有XDG_CONFIG_HOME,那么就会使用$HOME/.config/git/ignore文件里的配置。

3.语法规则如下:

  • 空行不匹配文件,仅作为行分割以便增加可读性

  • # 代表注释

  • \要用\转义

  • 末尾的空格会被忽略,除非用"\"引用

  • !代表取反,如果文件在前面已经被忽略,再用就表示包含。"\!important.txt"

  • /代表是个文件路径分隔符

  • 如果/在首位或中间使用,代表此匹配模式仅在当前根目录生效。否则在.gitignore文件层级一下都可生效。

  • /匹配模式末尾代表匹配全路径,否则目录和文件皆可匹配。比如doc/frotz/值匹配doc/frotz路径,但不匹配a/doc/frotz。frotz匹配文件夹frotz和a/frotz。foo/可以匹配文件夹foo,但不可匹配文件foo。

  • *匹配除/外的所有符号。?匹配一个字符除/外。区间匹配[a-zA-Z]可匹配一个在此区间的字符。

  • **/ 代表匹配所有路径,如**/foo

  • /**代表路径下的所有文件,如abc/**匹配所有abc目录下的文件

  • /**/代表匹配0或多个路径。如a/**/b匹配a/b,a/x/b,a/x/y/b等等

4.不生效的问题

gitignore文件的目的是确保没被跟踪的文件保持不跟踪,如果已经加入了git跟踪,则需要先停止跟踪,执行

git rm --cached

后即可去除跟踪。.gitignore文件就能生效了。

5.android gitignore模版

https://github.com/github/gitignore/blob/master/Android.gitignore

# Built application files
*.apk
*.aar
*.ap_
*.aab

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/
#  Uncomment the following line in case you need and you don't have the release build type files in your app
# release/

# 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/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml

# Keystore files
# Uncomment the following lines if you do not want to check your keystore files in.
#*.jks
#*.keystore

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild
.cxx/

# Google Services (e.g. APIs or Firebase)
# google-services.json

# Freeline
freeline.py
freeline/
freeline_project_description.json

# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md

# Version control
vcs.xml

# lint
lint/intermediates/
lint/generated/
lint/outputs/
lint/tmp/
# lint/reports/

# Android Profiling
*.hprof