31

I am having issues with the .iml files generated by Android Studio. On a Gradle sync, they are regenerated meaning I then have to do a commit even if nothing has changed. I just want to make these files untracked.

I have tried the following things.

  • Added *.iml to my project's .gitignore file as well as each module's .gitignore. I have tried both *.iml and **/*.iml
  • Used git rm --cached app/app.iml when they appear in the staged files list. Even after doing this and committing it, they appear as staged again later on.
  • As suggested here I added it to the Ignored Files in Settings under version control
Community
  • 1
  • 1
StuStirling
  • 15,601
  • 23
  • 93
  • 150

2 Answers2

44

You have the right steps, but you need to organize them

  1. git rm --cached <all_your_iml_files> to remove all of them from the remote repository.

    Alternatively you can do a simple command to delete all the *.iml files like git ls-files | grep "\.iml$" | xargs git rm --cached

  2. Commit that change using git commit -m "msg" and after that, you can see all your *.iml files as untracked files.

  3. Add *.iml to your .gitignore file and commit it in a separate commit or in the same previous commit.
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
  • 5
    I have done this multiple times. In fact I do this everytime they reappear but they are always re-staged – StuStirling Mar 02 '16 at 15:42
  • I just tried those steps on my terminal and they are working. Is the problem IDE related? – Ahmed Hegazy Mar 02 '16 at 15:48
  • I believe it's when `gradle` runs a sync, it regenerates the `.iml` files and stages them in GIT regardless of steps I have taken to prevent it – StuStirling Mar 02 '16 at 15:59
  • It can't stage them if they are not in the remote repository and added to the .gitignore at the same time. It must edit the .gitignore to do so. – Ahmed Hegazy Mar 02 '16 at 16:09
  • 3
    I just removed all my `.iml` files using `git rm --cached`, committed, pushed it to the remote repository's branch and then did a gradle sync and the .iml files were once again added to the staging... – StuStirling Mar 02 '16 at 17:27
  • I am having the same problem and this isn't working. Do I have to remove the file also from history? – IS1_SO Sep 03 '18 at 14:17
11

cd into project directory, git checkout and pull master branch

cd /home/your_user/project_directory
git checkout master
git pull origin master

edit .gitignore file to insert *.iml

git rm --cached **/*.iml
git commit -a -m "rm all *.iml, update .gitignore"
git push origin master

I was working on another maven & Java git project using Idea IDE and it seems to add *.iml in many of the child directories.

The glob syntax **/*.iml will cover all iml files in all directories within the current working directory.

James T.
  • 910
  • 1
  • 11
  • 24