Kotlin .gitignore template
The Kotlin template ignores compiled classes, packaged archives and Gradle state, and adds the .kotlin directory that the current compiler uses to keep session data.
At a glance
- Category
- Language
- Patterns in this template
- 19 rules
- Directories ignored
- 4 of the 19 rules end in a slash, so they match directories only
- Re-include rules
- 3 lines start with ! to keep something back
- Also needs an OS template
- Yes, and an IDE template too. Kotlin work almost always happens in an IDE that leaves its own files.
- Typical companions
- Gradle, plus IntelliJ IDEA or Android Studio and an OS template.
In detail
Kotlin compiles to the same class files as Java on the default target, so the *.class rule and the archive rules for jar, war, nar and ear carry over unchanged. Anything the compiler emits is derived from the sources plus the build configuration, and the archives are deliverables rather than inputs.
.kotlin is the addition worth knowing about. The newer compiler writes session and error data there, in the project root rather than inside the build directory, which means a project that only ignores build will start showing an untracked directory after a compiler upgrade. It holds nothing durable: deleting it costs one recompile.
The .gradle directory and build are the build system's working state. .gradle in particular holds file hashes and daemon state keyed to absolute paths, and copying it between machines produces cache misses at best. Both are regenerated from the build scripts, which are the files that actually describe the project.
The three negation lines are inherited from the Java lineage and matter for the same reason. Ignoring *.jar would also ignore the Gradle wrapper jar, which is the bootstrapper that lets a fresh clone run ./gradlew without anyone installing Gradle first. Committing that jar is the recommended practice, not an oversight, so the negation restores it.
hs_err_pid and replay_pid are virtual machine crash dumps. They are useful for exactly one debugging session and contain the full environment of the machine that produced them. On a multiplatform project, note that the native and JavaScript targets write into build as well, so the single build rule covers more than it appears to.
Examples
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
replay_pid*
.gradle/
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
.kotlin/19 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- The two negations for src/main and src/test build directories exist because build/ is unanchored. Remove them and a source package that happens to be named build disappears.
- Multiplatform projects also produce kotlin-js-store, which holds a Yarn lockfile for the JavaScript target. It is not covered here and is usually worth tracking rather than ignoring.
- Android modules add their own outputs on top of this. The Android Studio template covers local.properties and the capture directory that this one does not.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →Java .gitignore template
What the Java .gitignore template ignores: compiled classes, packaged archives, crash dumps, and the negations that keep the Gradle wrapper jar tracked.
Gradle .gitignore template
Six rules covering the Gradle cache and build directories, with negations that keep the wrapper jar and properties tracked because a clone needs both.
Android Studio .gitignore template
The Android Studio template ignores local.properties, which names the SDK path on one machine, along with build output, captures and native build directories.
Dart .gitignore template
The Dart template ignores the tool directory, generated docs and compiled JavaScript output, and ignores pubspec.lock, which an application should commit.
Scala .gitignore template
What the Scala template ignores: the target output directory, the build server protocol folder, and the caches that Metals and Bloop keep inside the project.
R .gitignore template
The R template ignores saved workspaces, session history, rendered vignettes and check output, plus the .httr-oauth file that stores live access tokens.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS