Git Best Practices and Terms
Good commits, branches, and reviews, keeping secrets out, protecting history, practices for database code, and the Git terms to know.
Commits
Git is easy to start with and easy to use badly. A few habits make the difference between a repository that's a clear, trustworthy record of every change, and one that's a confusing pile of "fixed stuff" commits with passwords hidden in its history.
- One logical change per commit
- A commit should do one thing, such as "add an index" or "rotate the backup schedule", so it's easy to review, understand, and revert. Use the staging area to split unrelated edits into separate commits.
- Commit often, in small steps
- Small commits are easier to review and to undo. A commit can always be squashed with others later; a huge commit can't easily be split.
- Write messages for the next person
- Start with a short summary, under about 50 characters, in the imperative mood, as if completing "This commit will…". Add a blank line and more detail if the why isn't obvious. Include the work item key so the change links to its ticket.
| Unhelpful | Helpful |
|---|---|
fixed stuff | DBA-142 Add index on orders(order_date, region) |
update | Raise sga_target to 8G on sales prod for month-end load |
changes from Friday | Grant finance_ro read access to orders |
Branches and reviews
- Never commit straight to
main. Every change goes through a branch and a pull request, even small ones. - Keep branches short-lived. Merge within a day or two, pulling
mainoften, to avoid painful conflicts. - Name branches clearly, with the work item key, such as
DBA-142-sales-report-index. - Protect
main. Turn on branch protection so that merging needs an approved review and passing checks, and nobody can force-push or delete it. - Keep pull requests small, with a description of what changed, why, how it was tested, and how to roll it back.
- Review database changes for database risks: locks on large tables, long-running migrations, missing rollback scripts, grants that are too broad, and changes that need a maintenance window.
Keep secrets out
Never commit passwords, private keys, wallets, or connection strings with credentials. Once a secret is pushed, it's in the history of every clone, and deleting the file later doesn't remove it. Treat any secret that reaches a remote repository as exposed: change it immediately.
- List secret-bearing files in
.gitignore, such as*.env, wallets, and localtnsnames.orafiles. - Keep secrets in a vault, such as HashiCorp Vault or your cloud's secret manager, or encrypted with a tool such as Ansible Vault, and have pipelines fetch them at run time.
- Turn on secret scanning, offered by GitHub and GitLab, or run a scanner such as gitleaks before each commit, to catch mistakes before they're pushed.
Protect the history
- Don't rewrite shared history. No
git push --forceon shared branches; usegit revertto undo pushed commits. - Pull before you push, so you're working on top of everyone else's latest changes.
- Tag releases, such as
v1.4.0, so you always know exactly which version is in production, and can go back to it. - Keep large files out. Data exports, dump files, and backups don't belong in Git; store them in object storage and keep only the scripts that create them.
Practices for database code
- Treat Git as the source of truth. If a change isn't in Git, it shouldn't be in the database. Manual "quick fixes" in production create drift.
- Never edit a migration that's already been applied. Add a new one instead, so every environment applies the same steps in the same order.
- Pair every change with its rollback where possible, and keep both in the same pull request.
- Organize the repository predictably, for example
migrations/,scripts/,ansible/,terraform/, anddocs/, with aREADME.mdexplaining how to use it. - Keep runbooks and documentation in the repository, next to the code they describe, so they're reviewed and updated together.
Git terms
- Repository (repo)
- A project folder tracked by Git, with its full history in the
.gitfolder. - Working directory, staging area
- Where you edit files, and where you collect the changes for the next commit.
- Commit
- A saved snapshot of the files, with an ID, author, date, and message.
- Hash (commit ID)
- The unique code that identifies a commit, such as
3f2a1c9. - Branch
- An independent line of work.
mainis the usual primary branch. - HEAD
- The commit and branch you're currently working on.
- Merge
- Combining the changes from one branch into another.
- Merge conflict
- When two branches change the same lines, and a person must decide the result.
- Rebase
- Replaying your branch's commits on top of another branch, for a straight-line history. Like amending, it rewrites history, so use it only on your own, unshared branches.
- Remote, origin
- A shared copy of the repository on a server.
originis the default name for the one you cloned from. - Clone, fetch, pull, push
- Copy a remote; download its new commits; download and merge them; upload your commits.
- Pull request (merge request)
- A proposal to merge a branch, with review and automated checks.
- Fork
- Your own copy of someone else's repository on the platform, common in open source when you can't push to the original.
- Tag
- A permanent label on a commit, usually a release version such as
v1.0.0. - Diff
- The line-by-line differences between two versions.
- Blame
- A view showing who last changed each line of a file, and in which commit.
- Revert
- A new commit that undoes an earlier one, safely, without rewriting history.
- Stash
- Temporarily shelving uncommitted changes, so you can switch branches and come back to them.
- Squash
- Combining several commits into one, often when merging a pull request.
- .gitignore
- A file listing patterns of files Git should never track.
- CODEOWNERS
- A file naming which people or teams must review changes to which paths.
- Branch protection
- Platform rules that stop direct pushes to a branch and require reviews and passing checks to merge.
In practice: the password in the commit
A DBA on Kofi's team commits a quick fix to the team's repository, and the script contains the production SYSTEM password. They notice an hour later and delete the line in a new commit. The password is still there: anyone with access to the repository can see it in the history, in every clone, and in every fork.
What the team does, in order:
- Change the password immediately. Treat it as exposed. Removing it from Git doesn't make it secret again.
- Check who could have seen it, and review the database's audit trail for unusual logins.
- Clean the history only if needed, with care, since it rewrites shared history and every clone must be refreshed.
- Prevent the next one: turn on secret scanning, add a pre-commit scanner such as gitleaks, and move the script to fetch the password from a vault.
The only reliable fix for a leaked secret is to make it useless, by changing it.
Key takeaways
- Make small commits, each one logical change, with clear messages that include the work item key.
- Work on short-lived branches, merge through reviewed pull requests, and protect
main. - Never commit secrets; if one is pushed, change it immediately. Use vaults and secret scanning.
- Don't rewrite shared history, tag releases, and keep large files out.
- For databases, Git is the source of truth: no manual changes, never edit applied migrations, and pair changes with rollbacks.