Android Studio中 .gitignore配置

前言

在用 Git 进行代码管理的过程中,我们会发现后添加进去的 .gitignore 文件,或者是中途修改了.gitignore 文件后,.gitignore 文件并没有生效,需要清楚.track缓存和强制.track来解决问题。如果我们在第一次提交的时候,忘记添加 .gitignore 文件或者在首次添加了 .gitignore 文件之后,又对 .gitignore 文件进行了修改,你会发现这两种情况下,.gitignore 文件是不生效的!

.gitignore 使用

我们第一次创建项目和上传项目的时候,是没有.gitignore文件的,我们需要自己配置,这里讲的是Android Studio中怎么使用.gitignore,关于第一次如何上传项目到gitlab,请参考gitlab入门篇
这里写图片描述
从以上提交的结果可以看出,没有添加 Android 工程应有的 .gitignore 文件,我们可以去https://github.com/github/gitignore下载一个,也可以拷贝之前的项目的.gitignore。然后我们将此 .gitignore 文件添加到gitlab仓库中。
这里写图片描述
注:如果你发现你添加了 .gitignore 文件之后,.gitignore 文件的变化没有被 track,那么你需要去 SourceTree 的全局 .gitignore_global 文件中查看 .gitignore 文件是不是被声明了不被 track的。
查看gitignore_global
一次选择工具–选项–git–Global Ignore List
接下来我们修改一个文件在尝试提交,就会发现没有之前那么多了。
这里写图片描述
从上图中我们可以看到 .java 文件编译产生的 .class 文件依然被 track。由于缓存的原因,当在提交之后添加 .gitignore 文件,或者在 .gitignore 文件中增加或者删除,都是无效的。
那么我们需要怎么做呢,我们需要清除track的缓存,首先我们可以通过 SourceTree 界面中的 Terminal 打开 Git 命令行终端。
这里写图片描述
输入清除命令

git rm -r –cached .

添加更改变化

git add .

备注:此命令,你可以通过 SourceTree 这样的可视化客户端来通过勾选 unstaged files 来操作。接下来输入命令:

git commit -m

操作完上面的步骤之后我们就会看到。
这里写图片描述
然后我们终于看到.gitignore 文件生效了。
这里写图片描述

其他

新增文件
实际应用中,我们往往随着业务的增加,会不断的增加文件,比如我们天津 .txt 文件,不让 .txt 文件被 track,我们还是按照刚才的步骤来操作即可!
这里写图片描述
我们就会发现现在sourcetree提交的时候就会忽略掉这种文件。
这里写图片描述
删除文件
比如,我们现在移除 .class 类型的文件,那么我们需要怎么做呢?
这里写图片描述
对于以上情况,我们需要用到强制提交的命令:

git add -f *.class

我们修改一下 .java 文件然后保存,看看产生的编译文件 .class 是否再次被 track,答案是肯定的!
这里写图片描述

说明:Git 代码管理中,我们在没有添加 .gitignore 文件的前提下提交了代码之后再提交 .gitignore 文件,或者是中途添加某一文件类型到 .gitignore 文件中,需要通过以下命令行的方式,让 .gitignore 文件生效:

git rm -r –cached .
git add .
git commit -m “Refresh adding .gitignore file.”

如果是中途从 .gitignore 文件中移除某一文件类型,想要这个文件类型重新被 track,需要通过以下命令行的方式,让 .gitignore 文件生效:

git add -f *.class
git commit -m "说明"

最后附上android 常见的一段.gitignore:

#built application files
*.apk
*.ap_


# files for the dex VM
*.dex


# Java class files
*.class


# generated files
bin/
gen/


# Local configuration file (sdk path, etc)
local.properties


# Windows thumbnail db
Thumbs.db


# OSX files
.DS_Store


# Eclipse project files
.classpath
.project


# Android Studio
.idea
#.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
.gradle
build/


# Signing files
.signing/


# User-specific configurations
.idea/libraries/
.idea/workspace.xml
.idea/tasks.xml
.idea/.name
.idea/compiler.xml
.idea/copyright/profiles_settings.xml
.idea/encodings.xml
.idea/misc.xml
.idea/modules.xml
.idea/scopes/scope_settings.xml
.idea/vcs.xml
*.iml

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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiangzhihong8

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值