← all lessons
Best Practices · lesson 18 of 18

The Dos and Don'ts

Git Best Practices Reference

Why it matters

After 17 lessons, here’s your cheat sheet. A comprehensive reference of everything you should and shouldn’t do with Git, organized by category.

Key concepts

The idea

The Master Reference

This lesson consolidates all the dos and don’ts from the entire curriculum. Use it as a quick reference when you’re unsure about best practices.

The Golden Rules

  1. Commit early, commit often—small commits are easier to understand and revert
  2. Never rebase public history—you’ll break everyone’s clones
  3. Never commit secrets—they’re in history forever
  4. Write meaningful commit messages—your future self will thank you
  5. Use branches for everything—main should always be deployable

Walkthrough

Commits

✅ DO❌ DON’T
Commit early and oftenBatch many changes into one commit
Write meaningful messagesUse “fix”, “update”, “WIP”
Use imperative mood: “Add feature”Use past tense: “Added feature”
Keep commits atomic (one purpose)Mix unrelated changes
Reference issues: Refs #42Leave commits orphaned

Commit Message Template

type: short summary (50 chars)

Longer explanation of WHY this change was needed.
The diff shows WHAT changed; the message explains WHY.

Refs #42

Branches

✅ DO❌ DON’T
Use feature branchesWork directly on main
Use descriptive names: feature/user-authUse vague names: my-branch
Delete merged branchesLet stale branches accumulate
Keep branches short-livedLet branches diverge for weeks
Pull/rebase from main regularlyWait until merge to discover conflicts

History

✅ DO❌ DON’T
Rebase local branches before pushingRebase pushed/shared branches
Use revert for public historyUse reset on public history
Squash before merging (if team prefers)Leave “fix typo” commits in PR
Use interactive rebase for cleanupForce-push to shared branches

Collaboration

✅ DO❌ DON’T
Pull before pushingForce-push without team agreement
Write clear PR descriptionsOpen PRs without context
Respond to review feedbackIgnore or argue with reviewers
Keep PRs small and focusedCreate 2000-line PRs
Test before opening PRPush broken code for review

Remotes

✅ DO❌ DON’T
Use SSH keys for authCommit with HTTPS and passwords
Fetch before making assumptionsAssume your local is current
Name remotes clearly (origin, upstream)Use confusing remote names
Push with -u to set upstreamManually specify remote every time

Security

✅ DO❌ DON’T
Use .gitignore for secretsCommit .env files, even “just for testing”
Use git-secrets or gitleaksRely on manual review
Sign commits on important reposAssume user.name is trustworthy
Rotate leaked credentials immediatelyJust delete the file and push
Enable GitHub secret scanningIgnore security alerts

Recovery

✅ DO❌ DON’T
Check reflog when panickingPanic and think data is lost
Use revert for pushed commitsUse reset --hard on pushed commits
Create backup branch before risky opsYOLO dangerous operations
Understand reset modes (soft/mixed/hard)Use --hard without thinking

Workflow

✅ DO❌ DON’T
Agree on team workflowMix Git Flow and GitHub Flow
Document your conventionsAssume everyone knows
Use CI/CD for testingRely only on local tests
Require PR reviewsMerge your own PRs
Use branch protection rulesAllow direct pushes to main

Organization

✅ DO❌ DON’T
Use clear issue descriptionsCreate vague issues
Link PRs to issuesLeave issues orphaned
Use labels and milestonesLet issues pile up without organization
Close issues with contextClose without explanation

Large Files & Scale

✅ DO❌ DON’T
Use Git LFS for binariesCommit large files directly
Use sparse checkout for monoreposClone entire monorepo unnecessarily
Clean up submodulesLet submodules go stale
Keep repo size reasonableLet history bloat with binaries

Key takeaways