github/gitignore · Java.gitignore
Java .gitignore Template Explained
Why Java.gitignore ignores .class files, package archives (jar, war) and JVM crash logs, and the ordering issue when combining it with build-tool templates.
Java.gitignore is a short template of about 13 lines that deals only with compilation output and files left by the JVM. It does not include Maven's target/ or Gradle's build/, so real projects usually combine it with the Maven or Gradle template and an IDE template.
The rule to watch most closely is *.jar. It is meant to keep distribution archives out of the repository, but it also ignores build-tool wrapper jars, so you need to pay attention to the order of the combination.
Rules explained
| Pattern | What it ignores and why |
|---|---|
*.class | Compilation outputBytecode that javac produces from source. It can be rebuilt at any time from the source, and its contents vary by compiler version. |
*.jar*.war*.nar*.ear*.zip*.tar.gz*.rar | Package archivesDistribution files and compressed archives produced by builds. The goal is to fetch dependency jars from a repository such as Maven Central instead of the old practice of putting them directly in the repository. |
hs_err_pid*replay_pid* | JVM crash logshs_err_pid<number>.log, which the JVM leaves in the working directory when it crashes, and replay_pid files for reproducing JIT compiler issues. They are one-off debugging files. |
*.log | LogsApplication run logs. Committing log files creates changes on every run and can leak operational information. |
*.ctxt.mtj.tmp/ | BlueJ and J2MEContext files from the educational IDE BlueJ and the temporary folder of Mobile Tools for Java (J2ME). They have no effect if you do not use those tools. |
Practical notes
*.jaralso ignores the Gradle wrapper'sgradle/wrapper/gradle-wrapper.jar. The Gradle template's!gradle-wrapper.jarsolves this, but because git follows the last matching rule, you must select them so that Gradle comes after Java.- Build folders (
target/,build/) are not in this template. Add the Maven or Gradle template. - IDE files such as IntelliJ's
.idea/and Eclipse's.projectand.classpathare handled by the JetBrains and Eclipse templates. - If you really must keep library jars in the repository, re-include only a specific path, such as
!libs/*.jar.
Original template
Java.gitignore
# Compiled class file*.class# Log file*.log# BlueJ files*.ctxt# Mobile Tools for Java (J2ME).mtj.tmp/# Package Files #*.jar*.war*.nar*.ear*.zip*.tar.gz*.rar# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xmlhs_err_pid*replay_pid*
Templates from github/gitignore/Java.gitignore @356fd7b (2026-09-11) · CC0-1.0