29

I am using Git in Android Studio on a OS X machine, and I would like to have a personal .gitignore file that overrides the .gitignore that are in the project (I want mine to ignore .iml files). Can this be done and how?

I have tried to create a .gitignore file in my home dir with the following lines in:

# Android Studio
.*.iml
*.iml

And the I have used this command to make git use my file git config --global core.excludesfile ~/.gitignore, but it does not work.

Any ideas?

pasbi
  • 2,037
  • 1
  • 20
  • 32
Neigaard
  • 3,726
  • 9
  • 49
  • 85
  • AFAIK, you cannot override the `.gitignore` file (if any) that sits in your repo's root directory, because it takes precedence over everything else, incl. any "core excludesfile". – jub0bs Sep 18 '14 at 12:38
  • Related: [Can you have multiple .gitignore files within a single repo?](http://stackoverflow.com/questions/9730486/can-you-have-multiple-gitignore-files-within-a-single-repo) – Sascha Wolf Sep 18 '14 at 12:51
  • No it did not work, I can not get it to ignore the .iml files :( – Neigaard Sep 24 '14 at 18:58
  • 2
    Possible duplicate of [git ignore files only locally](http://stackoverflow.com/questions/1753070/git-ignore-files-only-locally) – Randall Mar 11 '16 at 16:45

1 Answers1

43

Instead of creating a new .gitignore file, you should use the .git/info/exclude file to setup ignore rules specific to your clone of the repo.

So, basically, go to your project root, and run

cd $PROJECT_ROOT
echo "*.iml" >> .git/info/exclude

Note that the the pattern *.iml will take care of files of kind .*.iml as well, so you can make do with one ignore rule.

Also, this complements the existing ignore rules in .gitignore and ignore rules of .gitignore will still be applied.


It seems you are already tracking the .iml files in your Git repo, so you can try removing them from Git using

git rm -r *.iml
git commit -m "removed *.iml"

Note that this will untrack them from the master repository as well once you do a push.

Otherwise, you can use git update-index --assume-unchanged <filename> to ignore changes to those files locally. And afterwards, the gitignore rules should work all right.

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • Can this be used to override a `.gitignore` rule? (ex: overriding `*.jar` by using `!*.jar` in `.git/info/exclude`) – Stevoisiak Feb 28 '17 at 00:20