Elena' s AI Blog

Stop Committing Garbage: A Masterclass in .gitignore

30 Jan 2026 (updated: 30 Jan 2026) / 2 minutes to read

Elena Daehnhardt


Clean Code Environment, Midjourney 2026


TL;DR:
  • Don't let secret keys or system files clutter your repo. Learn how to write a robust .gitignore, use global ignores for macOS files, and remove accidentally tracked garbage.

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.

Introduction

The .gitignore file tells Git which files it should intentionally ignore. 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.

The Basics

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 Ignore: The files you ALWAYS ignore

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.

  1. Create a file at ~/.gitignore_global.
  2. Add your OS-specific junk there.
  3. 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 Mistakes: “I ignored it, but it’s still there!”

This is a classic “gotcha”. You add a file to .gitignore, but Git keeps tracking it.

Why? Because .gitignore only applies to untracked files. If you already committed config.env, Git will keep tracking it even if you ignore it later.

The Fix: You need to remove it 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.

Conclusion

Taking five minutes to set up a proper .gitignore saves you headaches down the road. It keeps your commits focused on code, not clutter.

Check your current project—are you tracking garbage? Time to clean it up!

Did you like this post? Please let me know if you have any comments or suggestions.

Git posts that might be interesting for you




desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.



Citation
Elena Daehnhardt. (2026) 'Stop Committing Garbage: A Masterclass in .gitignore', daehnhardt.com, 30 January 2026. Available at: https://daehnhardt.com/blog/2026/01/30/git-ignore/
All Posts