林小武

code coffee ukulele

  • 主页
  • 安卓
目录 关于我

林小武

code coffee ukulele

  • 主页
  • 安卓

git 学习笔记之 gitignore

2017-05-01

试过拉取代码后。用 Android Studio 编译了下。发现产生了几千个冲突。仔细一看,发现是 build 文件夹下的。原来的作者把编译文件都提交上去。其实也是个坏习惯。一个人维护项目。很多时候不会去注意太多。因为没有人和你产生冲突。所以就把代码都提交上去。结果只能在 .gitignore 上加忽略 build 文件下的以及其他不需要上传到远端的文件。但这样 git 的代码库其实已经越来越大了。

下面简单的说 gitignore 作用和 Android Studio 的 ignore 插件以及移除已经被误添加到 git 的指令。

.gitignore 的作用

无疑的。就是忽略这些文件。在 Java 中,我们不希望 .class 文件被提交了。还有在 Android 中,我们不希望编译出来的文件和 apk 之类的文件被提交上去。这时候就需要到在 .gitignore 里面加上代码去忽略这些文件。

下面是我个人 Android 项目下的 .gitignore 文件的代码:

1
2
3
4
5
*.iml
.gradle
/local.properties
/build
.idea

其实在 github 上,已经有很多设置好的 gitignore 无论是 C 还是 Python 上面都有。

其中 Android 的如下:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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/tasks.xml
.idea/gradle.xml
.idea/dictionaries
.idea/libraries

# Keystore files
*.jks

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

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

# Freeline
freeline.py
freeline/
freeline_project_description.json

# 是注释的意思。可以看到上面的忽略文件。基本上已经揽括了 Android 项目里面应该忽略的文件。

Android studio 的.ignore 插件

这个插件可以很方便的创建一些 ignore 文件

new_ignore

ignore_file_template
ignore_screen

这个插件有以下好处

  1. 可以创建选定模板。用的就是前面说过的 github 的 ignore 模板。
  2. 可以在 Android Studio 看到 global 文件和 exclude 文件。
  3. 可以看的出那些写的 ignore 是没用的。灰色字体和绿色字体区分。

移除命令

当我们不小心把编译的文件提交到 git ,但又不想删除本地的文件,可以通过以下的命令行去移除。

git rm --cached --force classfoo.com.txt

加上 –cached 表示不会在物理上去移除文件。

git rm -r --cached build/

加上 -r 表示删除这个目录下的文件的文件和该子目录的文件。

参考

  1. 在 git 中忽略文件 gitignore 与 exclude
  2. 忽略特殊文件
  3. git rm
  • git

扫一扫,分享到微信

微信分享二维码
打包 APK,Gradle,IDE插件,Python
我所理解的递归
© 2020 林小武
Hexo Theme Yilia by Litten
  • 目录
  • 关于我

tag:

  • Android开发
  • 随笔
  • 咖啡
  • RxJava
  • 音乐
  • TED
  • git
  • kotlin
  • java
  • 自动化
  • 算法
  • ukulele
  • 基础
  • Python

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

Android工程师
咖啡爱好者
会弹点尤克里里