Intermediate 20 min read

Branching and Committing Discipline

By the end of this lesson, you'll be able to:

  • Create and switch between branches to isolate work
  • Write a commit message that explains why, not just what
  • Explain why small, focused commits are easier to review and revert

Prerequisites: "Git Fundamentals for Developers"

Working on a branch

git checkout -b feature/add-loyalty-discount
# make changes...
git add .
git commit -m "Add loyalty discount calculation to CheckoutService"
git push origin feature/add-loyalty-discount

git checkout -b feature/add-loyalty-discount creates and switches to a new branch — an isolated line of work that doesn't affect main until it's explicitly merged. This means half-finished, experimental work never risks breaking what everyone else is using.

What makes a good commit message

BAD:  "fixed stuff"
BAD:  "updates"
GOOD: "Fix null check in enroll() that let a null Student crash the roster"

A commit message should explain why the change happened, not just restate the diff (which git show already displays). "fixed stuff" tells a future reader nothing; the good example tells them exactly what problem existed and what fixed it — genuinely useful six months later when nobody remembers the details.

Small, focused commits

BAD:  one commit that adds a feature, fixes an unrelated bug,
      and reformats three other files

GOOD: three separate commits, each doing exactly one of those things

A commit that mixes unrelated changes is hard to review (a reviewer can't approve just the bug fix without also approving the reformatting) and hard to revert (undoing the bug fix would also undo the feature). One commit, one logical change — this discipline pays off every time something needs to be reviewed, reverted, or understood later.

Exercise

As a comment, rewrite this bad commit message into a good one: "fixed bug". Assume the actual fix was correcting a division-by-zero in a discount calculator.

Show hint

Say what the bug was and what the fix does.

APEX

Branching and Committing Discipline Quiz

1. Why should a commit message explain why a change was made, not just what changed?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Branches let you develop a change in isolation before it touches shared code; disciplined, focused commits with clear messages turn history into something genuinely useful later.