Writing good commit messages in Git - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to write commit messages changes as the number of commits grows.
How does the effort scale when making many commits?
Analyze the time complexity of writing commit messages for multiple commits.
git add .
git commit -m "Fix typo in README"
git add .
git commit -m "Add new feature"
git add .
git commit -m "Update config"
This snippet shows making three commits, each with a message describing the change.
Each commit requires writing a message.
- Primary operation: Writing a commit message
- How many times: Once per commit
As the number of commits increases, the total time to write messages grows proportionally.
| Input Size (n commits) | Approx. Operations (messages written) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The effort grows linearly with the number of commits.
Time Complexity: O(n)
This means the time to write commit messages grows directly with the number of commits.
[X] Wrong: "Writing commit messages takes the same time no matter how many commits I make."
[OK] Correct: Each commit needs its own message, so more commits mean more messages and more time.
Understanding how tasks scale with input size helps you explain your work clearly and shows you think about efficiency in real projects.
"What if you used a single commit for many changes instead of many commits? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of commit messages
Commit messages should clearly explain what was changed and why, helping others understand the purpose.Step 2: Identify the best option
To explain what changes were made and why correctly states the purpose, while others focus on irrelevant or incorrect details.Final Answer:
To explain what changes were made and why -> Option BQuick Check:
Good commit message = explain what and why [OK]
- Listing files instead of explaining changes
- Writing too much unrelated history
- Including code in the message
Solution
Step 1: Recall Git commit message format
A good commit message starts with a short summary line, then a blank line, then optional details.Step 2: Match options to format
A short summary line followed by a blank line and detailed explanation matches this format exactly; others do not follow best practices.Final Answer:
A short summary line followed by a blank line and detailed explanation -> Option DQuick Check:
Summary line + blank line + details = correct format [OK]
- Skipping the blank line between summary and details
- Writing only a long paragraph
- Using emojis without explanation
Fix login bugWhat is missing to make it a good commit message?
Solution
Step 1: Analyze the commit message content
The message only has a short summary but lacks explanation of why the bug was fixed.Step 2: Identify what improves clarity
Adding a detailed explanation helps others understand the reason behind the fix.Final Answer:
A detailed explanation of why the bug was fixed -> Option AQuick Check:
Good message = what + why, missing why here [OK]
- Only writing a short summary
- Listing files instead of explanation
- Adding unrelated info like author email
fix login bugWhat is the main problem with this message?
Solution
Step 1: Check capitalization in commit messages
Good commit messages start with a capitalized summary line.Step 2: Evaluate the given message
The message starts with lowercase 'fix', which is not recommended.Final Answer:
It uses lowercase and lacks a capitalized summary -> Option CQuick Check:
Summary line should start capitalized [OK]
- Starting summary with lowercase letter
- Making summary too long
- Ignoring tense conventions
Solution
Step 1: Evaluate clarity and detail in options
Add caching to improve search speed Implemented a cache layer to store recent search results, reducing response time. has a clear summary and detailed explanation of what and why.Step 2: Compare other options
The other options are vague, use incorrect tense or verbs (such as 'Fix' for a new feature), or lack detailed explanation.Final Answer:
Add caching to improve search speed Implemented a cache layer to store recent search results, reducing response time. -> Option AQuick Check:
Clear summary + detailed why = best commit message [OK]
- Using vague summaries
- Wrong verbs like 'Fix' for new features
- Too short or unclear explanations
