Good Git Habits
Git rewards a few simple habits โ commit often with clear messages, ignore what shouldn't be tracked, and keep a tidy workflow. These small disciplines make version control genuinely pleasant.
You now know the mechanics of Git; this final session is about using it well. A handful of simple habits separate a tidy, useful Git history from a confusing mess - and they cost almost nothing once they're routine. Commit often with clear messages, keep junk out of your repo, and follow a simple workflow. These small disciplines make version control genuinely pleasant and turn your history into a real asset.
Commit often, commit clearly
The single best habit: make small, frequent commits, each doing one logical thing, each with a clear message.
Keep the repo clean
Not everything belongs in version control. A .gitignore file tells Git what to leave out.
# .gitignore - one pattern per line
__pycache__/ # compiled Python
*.log # log files
.env # secrets - NEVER commit these!
node_modules/ # downloaded dependencies| Do track | Don't track (gitignore it) |
|---|---|
| Your source code | Compiled/generated files |
| README, config templates | Secrets, passwords, API keys |
| Small necessary assets | Huge files, downloaded dependencies |
| Tests | Personal editor settings |
The most important rule: never commit secrets - passwords, API keys, tokens. Once something is committed and pushed, it's in the history (and possibly public) even if you delete it later. Put secrets in a file like .env and gitignore it, always.
Habits that pay off
- Commit often, small and logical, each with a clear message.
- .gitignore keeps generated files, dependencies and clutter out.
- Never commit secrets - keys, passwords, tokens (gitignore them).
- A tidy workflow - branch per feature, pull before you start - keeps things smooth.
๐ฏ Key takeaways
- The best habit: small, frequent commits, each doing one logical thing, each with a clear message.
- A readable history lets you find changes, undo one thing precisely, and understand the project - giant vague commits throw that away.
- Use .gitignore to keep generated files, dependencies and clutter out of the repo.
- Never commit secrets (keys, passwords, tokens) - once pushed they're in the history even if deleted; gitignore them.
Frequently asked
How often should I commit - is there such a thing as too often?
Lean toward committing often - most beginners commit too rarely rather than too frequently, and frequent small commits are a hallmark of good practice. The guiding principle is one commit per logical unit of work: whenever you've completed a coherent, self-contained change - fixed a bug, added a small feature, refactored a function, updated some docs - that's a natural point to commit. This might be several times an hour when you're productive, and that's completely fine and healthy. Frequent commits give you a fine-grained history (easy to read and to undo precisely), more save points to return to if something breaks, and smaller, easier-to-understand changes. Is there such a thing as too often? Only in the mild sense that a commit should represent something meaningful rather than, say, every single keystroke or a commit that leaves the code in a broken half-state for no reason - each commit ideally being a sensible, complete little step. But erring on the side of more, smaller commits is far better than the common beginner mistake of working for hours and then making one giant commit that mixes many unrelated changes with a vague message, which wastes most of Git's power. A practical rhythm: as you work, whenever you reach a point where you could describe what you just did in a short clear sentence ('Add email validation', 'Fix the total calculation'), commit it with that as the message, then continue. If you can't summarise it in a sentence, it might be that you've done several things and should have committed sooner. So: commit whenever you've finished a logical piece, don't wait for the whole feature to be done, and don't worry about committing 'too much' as long as each commit is a meaningful step. Frequent, focused commits are a good habit, not a problem.
What exactly are 'secrets' and why is committing them such a big deal?
Secrets are any sensitive credentials that grant access or prove identity - things like passwords, API keys, access tokens, private keys, database connection strings with credentials, and similar. Committing them is a serious and surprisingly common mistake for a crucial reason: Git keeps history. Once you commit a secret and push it, that secret isn't just in your current files - it's permanently recorded in the repository's history, in that commit, and remains there even if you later delete it from the current version. Someone looking through the history can still find it in the old commit. And if the repository is public (or ever becomes public, or is shared), that secret is now exposed to anyone, potentially including automated bots that specifically scan public repositories for leaked keys within minutes. A leaked API key can let attackers run up huge bills on your account or access your data; a leaked password can compromise systems. Because the secret is baked into history, truly removing it is difficult (it requires rewriting history, and you should assume any leaked secret is already compromised and needs to be changed/rotated immediately). The right approach is to never let secrets into Git in the first place: keep them in a separate file (commonly named something like .env), and add that file to your .gitignore so Git never tracks it. Your code reads the secret from that file (or from environment variables) at runtime, and the file stays only on your machine and the servers that need it, never in the repo. You commit a template or example (like .env.example with blank values) so others know what settings are needed, but never the real values. This is important enough that it's worth double-checking before your first push to any new project: make sure no passwords or keys are in the files you're committing. The habit of gitignoring secrets from the start prevents a whole category of security disasters, and it ties directly into broader security practices. In short: secrets are credentials, Git remembers everything forever, exposure can be immediate and costly, so keep secrets out of Git entirely by gitignoring them.
You've finished the Version Control with Git sessions
Now lock it in with the coursework.
Do the assignments โ