How to Ignore Compiled Output Files Like .class, .o, .exe, and .pyc Using GitHub's gitignore Templates
To ignore compiled output files in Git, copy the relevant patterns from the official language-specific templates in the github/gitignore repository—such as Java.gitignore for .class files, C.gitignore for .o objects, and Python.gitignore for .pyc bytecode—into your project's root .gitignore file.
The github/gitignore repository maintains authoritative templates that catalog every common build artefact generated by compilers and build systems. Instead of manually researching which files to exclude, you can reference these community-maintained templates to ensure compiled outputs never reach your version control history.
Locating Templates for Specific Compiled Outputs
The repository organizes ignore patterns by programming language and operating system. Each template contains precise glob patterns targeting compiler-generated files at specific line numbers.
Java, Kotlin, and Scala Bytecode
For JVM-based languages, compiled bytecode appears as .class files. In Java.gitignore, line 2 explicitly defines the pattern:
*.class
The same pattern appears in Kotlin.gitignore (line 2) and Scala.gitignore (line 1), ensuring coverage for all JVM language ecosystems.
C and C++ Object Files
Native compilation produces intermediate object files before linking. The C.gitignore template lists these at line 32:
*.o
*.obj
Similarly, C++.gitignore includes these patterns at line 38, along with additional C++-specific build artefacts. These rules handle object files generated by GCC, Clang, and MSVC compilers.
Windows Executables
For Windows development environments, compiled binaries carry .exe and .dll extensions. The Global/Windows.gitignore template provides these patterns at line 4:
*.exe
*.dll
Additionally, VisualStudio.gitignore (line 343) contains comprehensive Windows binary exclusions for Visual Studio project outputs.
Python Bytecode
Python interpreters compile source files to bytecode cached as .pyc files and __pycache__ directories. The Python.gitignore template defines these exclusions at line 4:
*.pyc
__pycache__/
Implementing Ignore Patterns in Your Project
You have two approaches for integrating these templates: selective copying for minimal configurations or full template inclusion for comprehensive coverage.
Minimal Combined Approach
For polyglot projects, concatenate only the specific patterns you need into a single .gitignore file:
# Java byte-code (from Java.gitignore line 2)
*.class
# C / C++ object files (from C.gitignore line 32)
*.o
*.obj
# Windows executables (from Global/Windows.gitignore line 4)
*.exe
# Python compiled files (from Python.gitignore line 4)
*.pyc
__pycache__/
Full Template Approach
For single-language projects, copy the entire template content to ensure you capture all edge cases, including secondary build artefacts like .jar files for Java or .so files for C:
# ==================== Java ====================
# https://github.com/github/gitignore/blob/main/Java.gitignore
*.class
*.jar
*.war
*.ear
# ==================== C / C++ ====================
# https://github.com/github/gitignore/blob/main/C.gitignore
*.o
*.obj
*.so
*.dylib
# https://github.com/github/gitignore/blob/main/C++.gitignore
*.obj
*.exe
*.out
# ==================== Python ====================
# https://github.com/github/gitignore/blob/main/Python.gitignore
*.pyc
__pycache__/
*.pyo
Verifying Pattern Effectiveness
After adding patterns, verify they match your compiled files using Git's check-ignore command:
git check-ignore -v path/to/your/file.class
This command outputs the specific .gitignore line number and pattern that causes the file to be ignored, confirming your configuration correctly targets the compiled output files.
Summary
- Use language-specific templates from
github/gitignorerather than maintaining custom patterns, as they are community-verified against current compiler versions. - Reference exact file paths like
Java.gitignore(line 2) for.class,C.gitignore(line 32) for.o, andPython.gitignore(line 4) for.pycto ensure authoritative patterns. - Combine templates by concatenating relevant sections when working with multiple languages in a single repository.
- Verify ignores using
git check-ignorebefore committing to confirm compiled outputs are properly excluded.
Frequently Asked Questions
Can I combine multiple gitignore templates in one project?
Yes. Polyglot repositories commonly concatenate sections from multiple templates. Place language-specific sections in your .gitignore with clear comments indicating their source, such as copying the *.class pattern from Java.gitignore alongside *.pyc from Python.gitignore. Git applies all patterns cumulatively.
Should I copy the entire template or just specific lines?
Copy specific lines for minimal, targeted ignores when you only need to exclude compiled outputs. Copy the entire template when you want comprehensive coverage of all build artefacts, including package manager directories and IDE configuration files that the full templates provide.
How are these templates kept current with new compiler versions?
The github/gitignore repository is maintained by thousands of community contributors who update templates as new build tools emerge. Patterns for established formats like .class and .o remain stable, while emerging toolchains receive updates through pull requests reviewed by language experts.
Do these patterns work for compiled files in subdirectories?
Yes. Gitignore patterns without leading slashes apply recursively to all subdirectories. The patterns *.class, *.o, and *.pyc from the official templates will match compiled output files regardless of their depth in the repository tree, ensuring nested build directories remain clean.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →