We’ve all seen it. You clone a repository, and there it is: a .DS_Store file, a __pycache__ folder, or worse—a file containing local API keys.
Committing these files is messy, unprofessional, and sometimes dangerous. The solution is simple but often misunderstood: the .gitignore file.
What Is .gitignore and Why It Matters
A .gitignore file is a plain-text configuration file that tells Git which files and directories to exclude from version control. It’s not just about keeping your repo tidy; it’s about security and collaboration. You don’t want to force your local editor settings or operating system junk on your teammates.
How to Create a .gitignore File in Git
Create a file named .gitignore in your project root. Add patterns for files you want to exclude:
# Dependencies
node_modules/
.venv/
# Compiled code
*.pyc
__pycache__/
dist/
# System files
.DS_Store
Thumbs.db
Now, git status won’t even show these files.
Global .gitignore: Excluding OS and Editor Files Across Every Repository
Some files, like macOS .DS_Store or editor configurations (.vscode/), haunt every project you touch. Instead of adding them to every single project’s ignore file, you can set up a global .gitignore.
- Create a file at
~/.gitignore_global. - Add your OS-specific junk there.
- Tell Git to use it:
git config --global core.excludesfile ~/.gitignore_global
Now, you never have to worry about accidentally committing a .DS_Store file again.
Fixing “.gitignore Not Working”: Untracking Already-Committed Files with git rm –cached
This is a classic “gotcha”. You add a file to .gitignore, but Git keeps tracking it.
Cause: .gitignore only applies to untracked files. If you already committed config.env, Git will keep tracking it even if you ignore it later.
Fix: Remove config.env from the index (staging area) but keep it on your disk:
git rm --cached config.env
Then commit that change. Now Git will respect your ignore rule.
.gitignore Key Takeaways for Clean Git Repositories
A well-maintained .gitignore is a version-controlled safeguard that keeps build artifacts, secrets, and OS clutter out of a repository’s permanent history, rather than a one-time setup step. Taking five minutes to set it up saves you headaches down the road, and it keeps your commits focused on code, not clutter.
Check your current project—are you tracking garbage? Time to clean it up! For the full pattern-matching syntax, see the official git-scm gitignore documentation, and browse ready-made language templates in GitHub’s gitignore repository.
gitignore FAQ
Why does .gitignore not work on a file I already committed?
.gitignore only affects untracked files. If a file was committed before you added it to .gitignore, Git keeps tracking it because it is already in the index. Run git rm --cached <file> to remove it from tracking, then commit — the ignore rule only takes effect after that.
What is the difference between .gitignore and git rm –cached?
.gitignore prevents untracked files from being added to Git in the future. git rm --cached removes a file Git is already tracking from the index without deleting it from disk — the step needed before .gitignore can take effect on that file.
How do I ignore .DS_Store and editor files in every project, not just one?
Create a global ignore file at ~/.gitignore_global, add patterns like .DS_Store and .vscode/, then run git config --global core.excludesfile ~/.gitignore_global so Git applies it across every repository on your machine.
Can .gitignore keep secrets like API keys out of my repository?
.gitignore prevents a secret file such as .env from being committed in the first place, but it cannot remove a secret that was already committed. If a key has already been pushed, rotate the credential and rewrite Git history — deleting the file in a new commit still leaves it recoverable from prior commits.
Did you like this post? Please let me know if you have any comments or suggestions.
Git posts that might be interesting for youEnjoyed this? Get more like it.
Weekly notes on AI tools, Python, and what I'm actually building — plus a free copy of Fantastic AI: The 2026 Toolkit.