git-hygiene
Weak git use looks like: git add -A && git commit -m "updates" on main, with a debug script and a .env riding along. Strong git use makes every commit a deliberate, reversible, explainable unit. Git is the safety net and the paper trail; each step below protects one or the other.
Before the work
- Check where you stand:
git statusandgit branch --show-current. Pre-existing uncommitted changes and stashes are the user's — not yours to stage, revert, or drop (see data-loss-guard). - Branch before risky or user-visible work, and never commit to main/master unprompted, even for "one small fix":
git switch -c <type>/<short-slug>in the repo's naming style. A branch costs nothing; un-committing from main costs a conversation. - Read history before "fixing" odd code:
git log -S <symbol> --onelineandgit blame -L <start>,<end> <file>on the lines you're about to change. If a past commit did it deliberately, its message says why — respect it, or cite it when overriding.
Staging
- Stage by explicit path, never by wildcard.
git add <file> ...orgit add -p; nevergit add -Aorgit add .. First list every untracked path ingit statusand decide each: intended artifact (stage), ignorable (add to.gitignoreif the repo would want that), or debris you created (delete). Anything untracked you didn't create gets asked about, not committed. - Scan the staged diff for secrets before committing:
git diff --cached | grep -inE 'api[_-]?key|secret|token|passw|PRIVATE KEY|AKIA[0-9A-Z]{16}'. A hit means unstage and remove. A secret in an already-pushed commit is compromised even after amend or rebase — tell the user to rotate it; never present history rewriting as the fix. - Read the staged diff top to bottom (
git diff --cached) — the last cheap moment to catch debug prints, commented-out experiments, and unrelated hunks (see workmanship). Unrelated hunks go in a separate commit or stay unstaged.
Committing
- Commit only when asked, then one logical change per commit. Run
git log --oneline -20first and match the house style (prefix, tense, length). The subject states what and why for the person runninggit logduring an incident: "fix utf8 filename crash in import", not "fix bug". Reasoning and behavior changes go in the body. - Don't rewrite history you don't own. Before
commit --amend,rebase,reset, or any force-push, rungit log origin/<branch>..HEAD --format='%h %an %s': every commit you'd touch must be unpushed AND authored this session. Otherwise stop and ask. If a force-push is explicitly requested, use--force-with-lease, never--force. - Never push, tag, or open a PR unprompted. When asked, name the exact remote and branch in the report.
Exit check
Report the branch, each commit hash and subject, and exactly what remains uncommitted or untracked and why. git status after your report should hold no surprises for the next person.


