KevsRobots Learning Platform
By , Minute
Projects collect files you never want to commit:
__pycache__ folders, compiled firmware, build folders.DS_Store on a Mac, Thumbs.db on WindowsSecrets are the important one. If you commit your Wi-Fi password and push to GitHub, it’s public, and it stays in the history even if you delete the file later.
You tell Git which files to ignore with a file called .gitignore (note the dot at the start) in the top folder of your repository. Each line is a pattern. Ignored files don’t show up in git status, and git add . skips them.
Let’s say our robot connects to Wi-Fi, and we keep the password in secrets.py:
# secrets.py - never commit this!
WIFI_SSID = "MyNetwork"
WIFI_PASSWORD = "hunter2"
Run git status --short and Git wants to track it:
?? secrets.py
Create .gitignore with this in it:
# Passwords and keys
secrets.py
.env
# Python
__pycache__/
*.pyc
# System junk
.DS_Store
Thumbs.db
Run git status --short again:
?? .gitignore
secrets.py has vanished from the list. Git is ignoring it. Notice that .gitignore itself shows up: you should commit it, so everyone working on the project ignores the same files.
git add .gitignore
git commit -m "Ignore secrets and Python cache files"
| Pattern | What it ignores |
|---|---|
secrets.py |
Any file called secrets.py, in any folder |
*.log |
Every file ending in .log |
build/ |
Any folder called build, and everything in it |
/todo.txt |
todo.txt in the top folder only, not in sub-folders |
docs/*.pdf |
PDFs directly in the docs folder |
!keep.log |
Don’t ignore keep.log, even though *.log matched it |
# comment |
Lines starting with # are comments |
You don’t have to write these from scratch. GitHub keeps a collection of ready-made ones at github.com/github/gitignore. The Python.gitignore one is a good starting point for MicroPython projects.
Other people (and future you) still need to know what goes in secrets.py. A common trick is to commit an example file with fake values:
# secrets_example.py
# Copy this file to secrets.py and fill in your own details.
WIFI_SSID = "your-network-name"
WIFI_PASSWORD = "your-password"
secrets_example.py doesn’t match the pattern secrets.py, so it gets committed.
.gitignore only affects files Git isn’t already tracking. If you committed a file before adding it to .gitignore, Git keeps tracking it. To stop tracking it but keep the file on your computer:
git rm --cached secrets.py
git commit -m "Stop tracking secrets.py"
Leaked a real password?
If you’ve already pushed a password or key to GitHub, change the password or revoke the key straight away. Removing the file in a new commit doesn’t remove it from the history, and bots scan GitHub for leaked keys within minutes.
If a file is mysteriously missing from git status, ask Git which rule is ignoring it:
git check-ignore -v secrets.py
.gitignore:2:secrets.py secrets.py
That means line 2 of .gitignore matched it.
secrets.py and a .gitignore that ignores it. Check git status.debug.log, and add a pattern to ignore all .log files..gitignore and a secrets_example.py.build with a file in it. Ignore the whole folder with one line..gitignore but Git still shows it as modifiedgit rm --cached <file> and commit.Why: .gitignore doesn’t apply to files that are already tracked.
.gitignore doesn’t seem to do anything.gitignore (not gitignore.txt) and is in the top folder of the repository.Why: Windows Notepad sometimes adds .txt to the end of file names.
.gitkeep.Quick quiz 1 of 3
Should you commit the .gitignore file itself?
.gitignore is part of the project’s setup, so commit it.Quick quiz 2 of 3
You committed secrets.py yesterday, then added it to .gitignore today. What happens?
Quick quiz 3 of 3
Which pattern ignores every .log file?
* matches any characters, so *.log matches every name ending in .log.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments