Ignoring Files with .gitignore
Summary: Prevent unwanted files from being tracked in Git.
Version control is essential for efficiently managing your codebase, and Git is one of the most popular tools for the job. However, not all files in your project should be tracked or shared—think of compiled binaries, temporary files, or sensitive configuration files. This is where the .gitignore
file comes into play. In this article, we'll explore how to use .gitignore
to keep your Git repositories clean and secure by preventing unwanted files from being tracked.
What is .gitignore
?
.gitignore
is a plain text file where you specify patterns for files and directories that you want Git to ignore. When you run git status
or try to commit changes, Git will skip any files or folders that match these patterns, unless they have already been tracked.
Why Use .gitignore
?
Tracking unnecessary files can cause a host of problems:
- Clutter: Non-essential files crowd your repository, making it harder to navigate.
- Security Risks: Sensitive information like API keys or passwords may be accidentally shared.
- Platform Issues: System-specific files (e.g.,
.DS_Store
on macOS,Thumbs.db
on Windows) should not be included. - Build Artifacts: Generated files (like binaries or minified JS) do not belong in source control.
By configuring .gitignore
, you ensure a tidy and portable repository, free from extraneous files.
How to Create and Use .gitignore
Creating a .gitignore
File
Simply add a .gitignore
file to the root of your repository:
touch .gitignore
You can also create additional .gitignore
files in subdirectories if you wish to ignore patterns only within certain folders.
Adding Rules
Inside the .gitignore
file, add one pattern per line. Here are some typical rules:
# Ignore all .log files
*.log
# Ignore build/ directory
build/
# Ignore files named "secret.txt"
secret.txt
# Ignore environment config (e.g., secrets)
.env
Note: .gitignore
rules are relative to the location of the file itself.
Wildcards and Patterns
*
matches any number of characters:*.tmp
will ignore all.tmp
files./
at the start specifies the root directory:/config.yml
only ignoresconfig.yml
at the root.!
negates the ignore rule:
This setup ignores all*.log !important.log
.log
files except forimportant.log
./**/
matches directories recursively:logs/**/*.log
ignores.log
files in anylogs
subdirectory.
Sample .gitignore
for a Node.js Project
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Optional npm cache directory
.npm
# dotenv environment variables file
.env
# macOS system files
.DS_Store
Files Already Tracked by Git
If you add a rule to .gitignore
but the file is already being tracked, Git will continue to track changes to it. To stop tracking a file:
git rm --cached filename
Replace filename
with your file or folder. Once removed from the index, Git will observe the .gitignore
rule for subsequent changes.
Using Global .gitignore
Some ignore patterns are useful for all your projects (like OS-specific files). You can configure a global .gitignore
file:
git config --global core.excludesfile ~/.gitignore_global
Add patterns to ~/.gitignore_global
. These will apply to all repositories on your machine.
Useful Resources
- gitignore.io: Generate language or framework-specific
.gitignore
templates. - Git Official Documentation: .gitignore
Conclusion
A well-maintained .gitignore
is vital for keeping your Git repository clean, secure, and manageable. By thoughtfully excluding inappropriate files, you protect sensitive data, avoid unnecessary clutter, and make collaboration easier. Whenever you start a new project, invest a few moments to configure your .gitignore
—your future self (and your collaborators) will thank you!