150

I saw a lot of examples for .gitignore files for AndroidStudio, some have .idea in them, and some don't.

Is there a good reason not to add the entire .idea dir to .gitignore?

If it should not be completely ignored, are there specific files inside .idea (such as .iml) that should be in .gitignore?

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
dors
  • 5,802
  • 8
  • 45
  • 71

4 Answers4

112

You can take a look at this page :

IntelliJ doc about project configuration files

In the "Directory-based format", a particular line is interesting :

The .idea directory contains a set of configuration files (.xml). Each file contains only a portion of configuration data pertaining to a certain functional area which is reflected in the name of a file, for example, compiler.xml, encodings.xml, modules.xml.

Almost all of the files contain information core to the project itself, such as names and locations of its component modules, compiler settings, etc. Thus, these files may (and should) be kept under version control.

However, I properly HATE to make project IDE-dependent (I am currently working on a project made with NetBeans and it hurts to use it with Eclipse which becomes the standard of my company).

So, to answer your question :

  1. If you do not use something like Maven or Gradle to manage dependencies and build : keep the directory under version control. This way, the correct configuration of the project and dependencies will be available for everyone. In the counterpart, all developers will have to set their environment exactly the same way that you define it in the config files.
  2. If you do use something like Maven or Gradle : correctly configure these tools and do not keep the directory under version control. Actually, all the information contained inside config files should be stored in Maven/Gradle files. Then let your developers configure their IDE depending of their environment. This way, using Eclipse, IntelliJ, Linux, Windows ... will not be a problem anymore.
Community
  • 1
  • 1
mithrop
  • 3,283
  • 2
  • 21
  • 40
  • 9
    Note the next paragraph, though: "The exception is the file workspace.xml. It stores your personal settings ... So it's unlikely that you'd want to share this file with your colleagues." – Dalbergia Jan 11 '16 at 16:19
  • 1
    It would be great if you added to your answer the required code of lines to do this in the gitignore file. Adding the correct instructions can be somewhat difficult to do for the new comers. – carloswm85 Nov 24 '20 at 12:11
43

OK, so after some "Yes" and "No" answers, I am adding a "Yes and no" answer :)

The problem is that .idea is used for both project build configuration (dependencies declaration) and project settings (inspections, etc.).

You definitely don't want to use your IDE for your build configuration, but you might want to share the settings among the team. That's why you need to ignore only a part of the .idea content (like the libraries folder and the modules.xml file), but keep others in the version control (e.g. the copyright, dictionaries and inspectionProfiles folders and files under .idea like dynamic.xml, codeStyleSettings.xml, etc.).

JBaruch
  • 22,610
  • 5
  • 62
  • 90
  • How specifically would address iml files? – dors Oct 13 '14 at 10:28
  • 1
    iml files should definitely be ignored. – JBaruch Oct 13 '14 at 11:23
  • 1
    I am still thinking that the configurations should not be kept in IDE-dependant files and so Maven/Gradle are so better to do it. – mithrop Oct 13 '14 at 15:29
  • @mithrop the thing is - you can't declare this type of configuration in Maven/Gradle file. It's Idea's own propriety format and it doesn't have portable alternatives in Maven/Gradle. – JBaruch Oct 13 '14 at 15:37
  • Oh yes, you're right. Anyway, if can't put it in maven/gradle files, you will have a problem with next time you import the project in another IDE (which is the problem I hate to handle). But I totally agree with you (if read my answer, you will see that I really am) : you include the files or not depending of your needs :) – mithrop Oct 13 '14 at 16:01
  • @mithrop our answers a bit different. I claim that even if you do you a proper dependency management, you still need to commit some of the files. – JBaruch Oct 13 '14 at 16:04
7

The concept of keeping the project configuration in VC is valid. I did this with my team because all of our developers happened to use PHPStorm for our projects and so it made sense to keep a common configuration ... in concept. We wanted to use the same dictionary files, the same coding standard rules, and the same plugin configurations.

The reason why I qualify this with "in concept" is because there were issues with JetBrains' .idea folder that led to us not being able to use it. These were probably issues that could have been avoided or fixed, but it was unclear to us how to do it right, and we think that's a fault of JetBrains because as developers we do not have time nor desire to search for solutions on how to make our IDE work correctly.

That being said, the issues were had are the following:

  • Symlinking project folders doesn't work right. When I set up my projects, I symlink them into my home directory. What we discovered was that the project was set-up to use the exact symlink rather than just treating it like a concrete directory. This means that if another developer keeps his project in a different place, or simply does not use symlinks, the entire directory will be missing from the project navigator because it is quite literally looking for the symlink. What's worse is that I could never find this path value in the configuration. We were unable to find the exact config in the files constituting our .idea folder.
  • Definition files are partitioned to users by default. This means if I want to add a word to my dictionary, it will be listed as a definition for me, jgreathouse, but other users will have their own definition section. The flagged words will still show up as a spelling mistake for other users. This is not desireable. The reason I add it to my definition file is because the IDE is wrong. I want these definitions to be intuitively shared with other users.
  • Colleagues kept overwriting the configurations because their IDE would overwrite the configurations with their config currently in Memory. What I mean is that, a developer would be working, and merge their repository from origin, which would contain a project configuration change, instead of their IDE changing configurations, or even giving them a choice, it would automatically overwrite the .idea configuration with the current in-memory configuration of their IDE. In my opinion this makes the .idea configuration unusable as a shared configuration. In order to work around this, the developer would literally have to shut down that instance of their IDE, pull the repo, and re-open their IDE. It makes no sense to keep a shared configuration if the IDE instantly overwrites it with the configuration currently in memory. It's like not having a shared configuration at all.

I've done these types of shared IDE configurations in VC before with Visual Studio and Netbeans and it was always fine; but with .idea it feels simply unusable which is disappointing. I wish JetBrains would get on top of it and make it a better user experience.

Jesse Greathouse
  • 382
  • 1
  • 4
  • 18
  • > instead of their IDE changing configurations, or even giving them a choice, it would automatically overwrite the .idea configuration with the current in-memory configuration of their IDE. Wow, that's really unfortunate. Good to know! – Greg Price Aug 07 '18 at 00:27
1

As a complement to the explanations in this question GitHub's .gitignore template for Android includes the following files:

# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml

Notice also this entry from JetBrains guidelines on How to manage projects under Version Control Systems about sharing IDE project files with other developers:

What needs to be shared:

All files under the .idea directory in the project root except the items that store user-specific settings: workspace.xml usage.statistics.xml shelf directory

AlbertSawZ
  • 51
  • 6
  • So maybe I'm missing something here, but if I exclude gradle.xml, how will users of my project know which gradleJvm to use? – Chucky Oct 26 '21 at 11:28