Git & GitHub ยท Chapter 41 of 42

Git Best Practices

Good Git habits make projects easier to maintain and collaborate on. This includes committing often with clear messages, using branches for new work, and keeping the main branch stable.

Following consistent practices โ€” like never committing secrets, reviewing diffs before committing, and pulling before pushing โ€” prevents many common problems on both solo and team projects.

Syntax
git diff --staged
git commit -m "clear, specific message"

Commit habits

Commit small, logical changes often, write clear messages, and review `git diff` before committing to avoid accidentally including debug code or unrelated changes.

Repository hygiene

Keep main always in a working, deployable state, use .gitignore to avoid committing generated files or secrets, and delete merged branches to keep things tidy.

Example 1 (bash)
git diff --staged
git commit -m "Add input validation to signup form"
Output
[main 6d2f0ab] Add input validation to signup form

Reviewing the staged diff before committing helps catch mistakes before they become permanent history.

Example 2 (bash)
git branch -d feature/old-experiment
Output
Deleted branch feature/old-experiment (was 4d1f0bb).

Deletes a merged, no-longer-needed branch to keep the branch list clean and manageable.

Key points

  • Commit small, focused changes with clear messages.
  • Review staged diffs before committing.
  • Keep main stable and always deployable.
  • Delete merged branches and never commit secrets.
๐Ÿ’ก Note: Good Git hygiene pays off most when a project grows or gains more contributors โ€” start good habits early.

๐Ÿ“ Quick Quiz

1. What is a recommended commit habit?

2. What should you review before committing?

3. What is good repository hygiene?