Skip to content

Instantly share code, notes, and snippets.

@TouristShaun
Last active December 5, 2023 16:28
Show Gist options
  • Save TouristShaun/4f63a3124a5d202f932b35dacd433a5f to your computer and use it in GitHub Desktop.
Save TouristShaun/4f63a3124a5d202f932b35dacd433a5f to your computer and use it in GitHub Desktop.

.gitignore Best Practices Guide

Overview
The .gitignore file is a crucial tool in any Git repository, helping to keep the repository clean by specifying untracked files and directories. Proper management of this file ensures a clutter-free, efficient, and collaborative working environment.

1. General Principles

  • Generalize When Applicable: Utilize wildcards (*) to exclude broad categories of files, such as *.log to ignore all log files.
  • Specify When Necessary: Use precise paths for unique situations, particularly for commonly named files not intended for tracking. For example, /todo.txt will ignore only the root level todo.txt.
  • Categorize Entries: Structure the file into clear sections (Logs, Dependencies, IDE/Editor Configs, etc.) for ease of navigation and maintenance.
  • Explain with Comments: Incorporate comments (#) to clarify the rationale behind ignoring certain files, especially if their exclusion isn't immediately apparent.
  • Review and Adapt Regularly: Update the .gitignore as your project evolves to reflect new or obsolete files and directories.
  • Global vs. Local: Place personal configurations and OS-specific files in your global .gitignore, not in the project's file.
  • Preserve Essential Files: Ensure vital files (like .env.example) for project setup are not ignored.
  • Leverage Community Resources: Utilize .gitignore templates from platforms like GitHub as a starting point for specific languages and frameworks.
  • Exclude Runtime Generated Files: Generally ignore files created during execution or compilation (e.g., node_modules, bin/, obj/).
  • Maintain Clarity: Avoid redundant or outdated entries to prevent confusion, particularly for new contributors.

2. Typical Patterns

  • Language-Specific Files: Include patterns for compiled files, logs, and temporary files specific to the languages used (e.g., *.pyc for Python).
  • Development Tools: Ignore configuration files for IDEs and editors (e.g., .vscode/, .idea/).
  • Build Outputs: Exclude directories where build outputs are stored (e.g., /dist, /build).
  • Dependency Folders: Common folders like node_modules/ or vendor/ should be ignored in most cases.
  • Environment Files: Exclude environment-specific files like .env.local or .env.development.
  • Logs and Databases: Ignore log files and local databases (e.g., *.log, *.sqlite).

3. Implementation Tips

  • Debugging Rules: Use git check-ignore -v <file> to troubleshoot and understand why specific files are ignored or not.
  • Regular Checks: Perform frequent git status checks to confirm that no unintended files are being tracked or omitted.

By adhering to these guidelines, your .gitignore file will effectively contribute to maintaining a well-organized and transparent repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment