Copyright © 2023 - Azat's Blog Azat's Blog
Introduction to .gitignore
The .gitignore file is an integral tool for developers using Git, ensuring that certain files or directories are excluded from the version control system. These exclusions help in keeping the repository clean from unnecessary or confidential files.
Why .gitignore is essential for your projects
- Prevents accidental check-in of sensitive data.
- Avoids clutter by excluding auto-generated or temporary files.
- Ensures a consistent codebase across development environments.
Difference between project-specific and global ignores
- Project-specific ignores: Relevant to a particular project and reside at the project root.
- Global ignores: Personal or system-specific files that are not related to a project, and are generally configured at the system level.
Setting up a global .gitignore
- Create a
.gitignore_global
file in your home directory. - Populate the file with general ignores. For example:
### OSX ###
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
### Jetbrains ###
.idea
### Node ###
node_modules
### Python ###
*.pyc
### CMake ###
cmake-build-*/
### Visual Studio Code ###
.vscode
### Config Files ###
.env
**/.env
### My Personal Notes ###
**/_azat
Code language: PHP (php)
- Configure Git to use this file globally:
git config --global core.excludesfile ~/.gitignore_global
Code language: PHP (php)
Incorporating project-specific ignores
Django’s approach serves as a prime example. The repository contains a .gitignore
file, and it only includes exclusions relevant to the Django project, ensuring clarity and cleanliness.
# If you need to exclude files such as those generated by an IDE, use
# $GIT_DIR/info/exclude or the core.excludesFile configuration variable as
# described in https://git-scm.com/docs/gitignore
*.egg-info
*.pot
*.py[co]
.tox/
__pycache__
MANIFEST
dist/
docs/_build/
docs/locale/
node_modules/
tests/coverage_html/
tests/.coverage*
build/
tests/report/
Code language: PHP (php)
Tips for effective .gitignore practices
- Regularly review and update .gitignore files.
- Avoid adding large binaries, even if they’re ignored. Instead, use tools like
git-lfs
. - Use online tools and templates for generating initial .gitignore files.
- Prioritize clarity; keep project-specific and global ignores separate.
Conclusion
Effectively using .gitignore, both at the project level and globally, can lead to a tidier, more efficient, and secure version control experience. By segmenting your ignores, you ensure that your repositories remain clear of unrelated or sensitive files.