如何在 IntelliJ 中在开发人员之间共享代码样式设置

我希望我的团队中的所有开发人员使用相同的默认代码样式设置。我们都使用 IntelliJ11 + 作为 IDE,并使用 git 作为源代码控制系统。

确保它们都使用相同设置的最简单方法是什么?我认为有一种方法可以将样式设置检入到项目中,并让他们的编辑自动发现它们,但事实似乎并非如此。

附言。我不介意开发人员有意识地用他们自己的首选项覆盖一些默认设置,但我确实想确保我们都至少从一组共同的默认设置开始。

30896 次浏览

Code Style can be copied to project and saved in .idea/codeStyles to be shared via version control:

Copy to Project Click this button to create a copy of the current global scheme to the project level. After creating the copy, IntelliJ IDEA suggests to switch to this new scheme at the project level.

The Settings Repository feature was introduced at IntelliJ IDEA 2016.

This option helps us to share IDE settings between different computers, including sharing settings between developers.

The settings are stored at Git repository, for example on GitHub or Bitbucket.

To setup Git repository we should set URL via Settings Repository menu option.

Calling settings

The developer can load remote settings, overwrite remote settings or merge local settings with remote ones.

Set url and choose action

The structure of Git repository with settings:

Git repository structure

I used personal access token for GitHub authentication.


More information:

I came across this long after the fact, but thought I'd share if anyone ran into this. Add the following to your .gitignore

# IDE - IntelliJ
/.idea/*
# Keep the code styles.
!/.idea/codeStyles
/.idea/codeStyles/*
!/.idea/codeStyles/Project.xml
!/.idea/codeStyles/codeStyleConfig.xml
# Keep the inspection levels
!/.idea/inspectionProfiles
/.idea/inspectionProfiles/*
!/.idea/inspectionProfiles/Project_Default.xml

And of course, make sure your .gitignore also has a ! in front of it so these changes get picked up.

Basically, gitignore's recursive looking is a little wonky, so the below ignores a directory's contents, except for a subdirectory, then ignores that subdirectory's contents, except for the files we want.

codeStyleConfig lets you use per project settings, the project file itself is your actual code styles, and I included the Project_Default as it holds the warning levels, which you likely want if you're doing the code style work anyways.

You can create .editorconfig file in Your project (and it can be managed on directory level). More info on https://www.jetbrains.com/help/idea/configuring-code-style.html#editorconfig and https://editorconfig.org/

With this approach You can keep all Your code style settings in one file and it's not limited to IJ only.