18

I have an Android library and for some reason, the files and folders I have specified in my .gitignore are not being ignored.

I have tried modifying my .gitignore and also following these steps, but this doesn't change anything.

Here is my top-level .gitignore (which can also be found on the GitHub repo):

# Gradle files
.gradle/
build/
*/build/

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

# IntelliJ
*.iml
/.idea

The module with the build folder that isn't being ignored has the following .gitignore:

/build/

I'm not sure why the build directory isn't being ignored, as it is being ignored in my sample app module, and in the top-level directory.

Also, I did commit changes to some files in the build directory when I updated versions of my library, if that's important.

Community
  • 1
  • 1
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125

4 Answers4

47

This answer on Stack Overflow helped me solve my issue.

Here is part of that answer:

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"

Community
  • 1
  • 1
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
3

You must remove the first / in your lower level gitignore before build, it will work then. Also, in top level, you only need this: build/ and then no lower level gitignore will be needed.

Jekabz
  • 41
  • 3
1

Add .gitignore file in your project, and set below lines:

*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/build
/captures
.externalNativeBuild
.idea

enter image description here

Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
1

In my case the .gitignore file was placed inside app directory instead of being in root of project directory.

I figured this out by:

  1. Open Project View in Android Studio
  2. Right-click on build folder
  3. Select Git
  4. Add to .gitignore
Safeer
  • 1,407
  • 1
  • 25
  • 29