Have you ever accidentally committed sensitive information like passwords, configs, or large files to your Git repository? If the answer is yes, then you need to learn how to handle the .gitignore
file. In this article, I'll walk you through how to create a .gitignore
file in a Git repository and optimize it for different tech stacks.
What is a .gitignore file?
The .gitignore
file is a special file in Git that lets you specify which files or folders should be ignored by Git when committing to your repository. In other words, anything listed in this file will be excluded from version control.
How to create a .gitignore file?
To create a .gitignore
file in a Git repository, run this command in your project root:
touch .gitignore
Then, open the file in your favorite code editor to add the rules.
Basic .gitignore file example
# Ignore dependency files
node_modules/
# Ignore environment variables
.env
# Ignore logs
*.log
How do .gitignore rules work?
The rules in .gitignore are based on simple patterns:
Pattern | Description | Example |
---|---|---|
name/ | Ignores a full folder | node_modules/ |
.ext | Ignores all files with that extension | |
name.ext | Ignores a specific file | config.local.php |
!file.ext | Excludes a file from being ignored | !important.log |
- | - | - |
Global .gitignore configuration
Besides the local .gitignore
, Git also lets you use a global .gitignore
file to apply rules across all your projects:
git config --global core.excludesfile ~/.gitignore_global
You can place in this file common rules used in every project, like:
# Mac OS
.DS_Store
# Temporary files
*.tmp
Practical .gitignore examples by stack
Here are ready-to-copy examples based on the tech stack you're using:
Laravel
/vendor/
/node_modules/
.env
Homestead.json
Homestead.yaml
.phpunit.result.cache
NodeJS
node_modules/
dist/
.env
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Python
__pycache__/
*.py[cod]
.env
*.sqlite3
You can find a full collection of templates here: github/gitignore
Best practices for using .gitignore
- Use templates: Don’t reinvent the wheel. Use proven templates like the ones on GitHub.
- Check files before committing: Always check which files you're about to commit using
git status
. - Be careful with sensitive data: Files like
.env
should never go into the repo. - Keep it updated: Revisit your
.gitignore
regularly and adapt it as your project grows.
Files you should never forget in your .gitignore
- Environment variables (.env, .env.local)
- Dependencies (node_modules, vendor)
- Local databases (*.sqlite3, *.db)
- Logs (*.log)
Recommended full example:
# Environment variables
.env
.env.local
# Dependencies
node_modules/
vendor/
# Local databases
*.sqlite3
*.db
# Logs
*.log
FAQ
1. What happens if I add a file to .gitignore after it’s already committed? Git will still track the file unless you remove it with:
git rm --cached filename
Then, it will be ignored in future commits.
2. Can I ignore a folder but keep a file inside it? Yes. For example:
folder/*
!folder/keepme.txt
This ignores everything in folder except keepme.txt.
3. Is .gitignore case-sensitive? Yes, on case-sensitive file systems like Linux. On Windows or macOS, behavior may differ.
4. Where should I place the .gitignore file?
At the root of your repository. You can also have nested .gitignore
files inside subdirectories if needed.
5. Does .gitignore affect files already in the remote repository? No. It only applies to local changes. You must manually remove files from the repo if you want them gone.
Conclusion
A well-configured .gitignore
file is key to keeping your Git project secure, organized, and clean. Creating a .gitignore
file in a Git repository using these tips will help you avoid common mistakes and work more efficiently.
Want to go deeper? Here's GitHub's official documentation on ignored files and advanced patterns.
Did you find this article helpful? Share it and help me reach more developers like you!
Happy coding 😎