๐ŸŒฟ
๐ŸŒฟ Version Control with Git ยท GIT-101 ยท Session 6 of 6

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.

โฑ 8 min read๐Ÿ“… Updated 2026-07-31๐Ÿ“Š Tables, charts & examples

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.

๐Ÿ“Œ Worked example
The API key that cost $5,000. A developer commits their code to a public GitHub repo, not noticing that a config file contains a live cloud API key. Within minutes - not days, minutes - automated bots that constantly scan public repos for leaked keys find it, and by morning the key has been used to run up thousands of dollars of charges. Deleting the key from the current files didn't help: it was still sitting in the commit history for anyone to find, so it had to be revoked and replaced entirely. The whole disaster is prevented by one habit: put secrets in a file like .env and add it to .gitignore, so they never enter Git at all. Never commit secrets - Git remembers everything, forever.

Commit often, commit clearly

The single best habit: make small, frequent commits, each doing one logical thing, each with a clear message.

๐ŸŽฏ
Small, frequent, well-described commits are the foundation of a useful history. A commit per logical change - 'Add login form', 'Fix off-by-one in pager', 'Update README' - gives you a history you can actually read and use: you can find when something changed, undo one specific change without touching others, and understand the project's evolution. The opposite - giant commits mixing ten unrelated changes, with a message like 'stuff' - throws away everything Git offers. It costs seconds to commit a small chunk with a clear message, and it pays back every time you need to read, search, or undo your history.

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 trackDon't track (gitignore it)
Your source codeCompiled/generated files
README, config templatesSecrets, passwords, API keys
Small necessary assetsHuge files, downloaded dependencies
TestsPersonal 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.

๐ŸŽ“ Final session complete

You've finished the Version Control with Git sessions

Now lock it in with the coursework.

Do the assignments โ†’

Related lessons

More in this course

Course home → · Browse all courses →