Search code examples
gitscalaintellij-ideasbtgitignore

Scala on IntelliJ with Git: How should the .gitignore look like?


Note: This question is specific to Scala projects. I want to have Scala compile and run successfully from inside IntelliJ without any prior configuration.

I have created a test project containing Scala examples using IntelliJ IDEA and published it to GitHub.

The project structure looks like this:

Project Structure

My current .gitignore looks like this:

*.class
*.log
target/
.idea/
project/

This results in a repository that looks like this:

GitHub repository structure

Now for what I am trying to do:

Having Scala example code is cool, but I want to also use this project as a template for Scala projects with IntelliJ.

So, how should I best change my .gitignore file, so whenever I clone the project, I can open it with IntelliJ and have everything in working order? This, of course, excludes target directories, so I need to recompile the project whenever I clone it.


Solution

  • Assuming that you're using SBT to build your project in IntelliJ, you should filter out the following directories:

    project/target/
    target/
    

    (Actually, just adding target/ filters both out.)

    These directories contain SBT output generated from your sources, and should not be placed under version control.

    If everyone working on your project is to use IntelliJ while working on the code, then you should add all of the ./.idea directory with the following exceptions:

    .idea/.name
    .idea/libraries/
    .idea/modules/
    .idea/modules.xml
    .idea/scala_compiler.xml
    .idea/tasks.xml
    .idea/workspace.xml
    .idea_modules/
    

    (You might also want to consider adding .idea/sbt.xml to this list. It has some redundant information from SBT, but some IntelliJ SBT configuration settings as well. Up to you...)

    These files and directories either contain information gleaned by IntelliJ from SBT (and are, therefore, redundant) or contain machine- and/or user-specific information which will create problems if checked out on a different machine or by a different user.

    If SBT is the primary build tool, and people can use any IDE they like, it would probably be better to ignore the entire .idea directory instead.