
Keeping Your Repo Clean: The Right .gitignore for Flask Projects | Python Comeback Journey #7
When I started committing my Flask app to Git, everything looked fine at first — until I noticed dozens of strange files appearing in the repo: __pycache__ folders, .env files, and compiled bytecode. It was cluttered, confusing, and a little embarrassing.
That’s when I realized it was time to learn about one of the most quietly powerful tools in every developer’s workflow: .gitignore.
🧩 Step 1: Understanding the Problem
By default, Git tracks everything in your project folder — even temporary or machine-specific files. That includes caches, virtual environments, logs, and configuration files.
In a Flask app, that quickly becomes messy:
|
1 2 3 4 5 6 7 |
MY_FIRST_FLASK_APP/ │ ├── __pycache__/ ├── instance/ ├── venv/ └── .env |
You never want those committed to your repository. They either waste space, expose sensitive data, or change constantly.
⚙️ Step 2: Adding a .gitignore File
A .gitignore file tells Git what not to track. It’s simple but incredibly effective. My initial version looked like this:
|
1 2 3 4 5 |
__pycache__/ *.pyc instance/ .env |
That was a good start — but it could be improved.
🧠 Step 3: Refining the Rules
After some research and discussion, I expanded it to a safer, more complete version:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Byte-compiled / cache files __pycache__/ *.py[cod] *$py.class # Environment files .env .env.* # Instance folder (Flask-specific) instance/ # Virtual environments venv/ # Logs and temporary files *.log *.tmp # System files .DS_Store Thumbs.db |
This new version filters out everything unnecessary while keeping the actual source code intact.
The *.py[cod] pattern covers all Python-compiled file endings (.pyc, .pyo, .pyd), while *$py.class filters platform-specific cache files.
🧭 Step 4: Why You Should Commit Your .gitignore
The irony of .gitignore is that it’s one of the most important files to commit.
It’s the rulebook that ensures your whole team (or your future self) doesn’t accidentally track junk. Every clone of the repo should have the same protection.
Git ignores files listed in .gitignore, not the .gitignore file itself.
So yes, you absolutely commit it:
|
1 2 3 4 |
git add .gitignore git commit –m “Add initial .gitignore for Flask project” git push |
Now anyone who pulls your repo automatically inherits the same safety net.
💡 Step 5: Reflection
This lesson wasn’t about Flask directly — it was about discipline. The more I code, the more I realize that good development isn’t just about writing new features. It’s about maintaining clarity, safety, and reproducibility.
A clean repo is a kind of respect — for your work, for your collaborators, and for the future version of yourself who will someday come back to this project.
Next up in the series, I’ll tackle something more visible: data persistence. It’s time to move beyond hardcoded lists and let the app load its data from a real file.
Stay tuned for Python Comeback Journey #8: From Hardcoded Lists to JSON Files — Giving Flask Apps Real Data.





Leave a Reply