A coherent commit groups one meaningful change that can be reviewed and recovered independently. It should preserve the relevant working state.
Before you start
You should understand commits, branches, the working tree and the staging area. Draw the commit graph before changing history. Try commands in a disposable repository with a clean working tree so you can observe exactly which references and files each operation changes.
The practical goal is to reason through this situation: Separate a behavior fix from unrelated formatting so the reason for each diff remains clear. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Define one outcome
Group files necessary for a coherent behavior change.
Step 2: Exclude unrelated work
Separate formatting and other edits when they obscure the review.
Step 3: Review the snapshot
A useful commit should stand on its own for validation and recovery.
Worked scenario
Separate a behavior fix from unrelated formatting so the reason for each diff remains clear.
A validation fix includes its code, user-facing message and meaningful regression check. Unrelated formatting across many files makes the actual decision harder to review. Splitting too finely can also create broken intermediate commits; coherence matters more than either smallest possible diff or largest convenient batch.
Common mistake
Tiny commits are not automatically useful if each leaves the project broken.
Verify the behavior
Read the staged diff as a reviewer and verify the committed state works independently.
Interview exercise
Prepare a focused change.
Answer and reasoning
Review scope, explain the outcome and include necessary associated files while excluding unrelated local work.
Continue learning
Compare the scenario with the Git and CI/CD interview questions and test your understanding with the Git and CI/CD MCQs. For terminology and implementation details, consult the reference material.