Day 19: Setting .gitignore Ignore List
When developing a project, new files may frequently appear in the working directory (such as temporary files or cache files generated by development tools like Visual Studio). Many of these files do not need to be included in version control and should be excluded. We call this the “ignore list.”
Understanding .gitignore
A .gitignore file specifies intentionally untracked files that Git should ignore. In Git, the .gitignore file is used to define the “ignore list.” The main purpose is to exclude files that don’t need to be added to version control. Files or paths (which can include wildcards) listed in this file will not appear in git status results, and naturally won’t be added when executing git add.
However, this only applies to Untracked files. Files already tracked by Git (Staged files) are not affected by the .gitignore file.
Creating .gitignore Through GitHub
If you’ve ever created a project on GitHub, you may have used this feature. When creating a new repository, you can select a pre-defined ignore list that GitHub helps you set up. This ignore list is actually just a file named .gitignore, placed in the project root directory by default.
You can select from various templates like:
- Node
- Python
- Java
- C#
- Visual Studio
- And many more
Creating .gitignore Manually
Simply create a file named .gitignore in your repository root:
# On Windows
echo node_modules/ > .gitignore
# On Linux/Mac
echo "node_modules/" > .gitignore
Or create it manually with a text editor.
Basic Patterns
Ignore Specific Files
config.local.js
secrets.txt
Ignore Files with Specific Extensions
*.log
*.tmp
*.cache
Ignore Directories
node_modules/
dist/
build/
Note the trailing slash to indicate a directory.
Ignore Files in Any Directory
# Ignore all .log files in any directory
**/*.log
Advanced Patterns
Negation (Exception)
Use ! to make exceptions:
# Ignore all .log files
*.log
# But don't ignore important.log
!important.log
Ignore Files in Specific Directory Only
# Ignore test.txt only in root directory
/test.txt
# Ignore test.txt in any subdirectory, but not root
*/test.txt
Multiple Extensions
# Ignore multiple file types
*.{jpg,png,gif}
Common .gitignore Patterns
Node.js Projects
node_modules/
npm-debug.log
.env
.DS_Store
dist/
build/
Python Projects
__pycache__/
*.py[cod]
*$py.class
.venv/
venv/
*.egg-info/
.pytest_cache/
Java Projects
*.class
*.jar
*.war
target/
.gradle/
build/
.NET Projects
bin/
obj/
*.user
*.suo
packages/
.vs/
Global .gitignore
For patterns you want to ignore across all repositories:
git config --global core.excludesfile ~/.gitignore_global
Then create ~/.gitignore_global with your patterns.
Ignoring Already Tracked Files
If you accidentally committed files that should be ignored:
# Remove from Git but keep the file
git rm --cached filename
# Remove directory from Git but keep it
git rm -r --cached directory/
Then add the pattern to .gitignore and commit.
Viewing Ignored Files
To see which files are being ignored:
git status --ignored
Checking Why a File is Ignored
To see which pattern is causing a file to be ignored:
git check-ignore -v filename
Template .gitignore Files
GitHub provides template .gitignore files for various languages and frameworks:
- https://github.com/github/gitignore
Comments in .gitignore
Use # for comments:
# This is a comment
*.log # Inline comment
Best Practices
- Commit .gitignore early: Add it at the start of your project
- Be specific: Avoid overly broad patterns
- Document why: Use comments to explain non-obvious patterns
- Keep it organized: Group related patterns together
- Use templates: Start with language-specific templates
- Local variations: Use
.git/info/excludefor personal patterns
Subdirectory .gitignore
You can have .gitignore files in subdirectories. Patterns are relative to the location of the .gitignore file.
Today’s Summary
The .gitignore file is essential for keeping your repository clean and avoiding accidentally committing files that don’t belong in version control. Understanding the pattern syntax and best practices will help you maintain a clean, professional repository.
Let me reorganize the Git commands and parameters learned today:
- git rm –cached
- git rm -r –cached
- git status –ignored
- git check-ignore
- git config core.excludesfile
Reference Links
- Back to Table of Contents
- Previous Day: Fixing Committed Version History Part 1 (reset & amend)
- Next Day: Fixing Committed Version History Part 2 (revert)