git log formatting options - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to run git log with formatting options changes as the number of commits grows.
How does adding more commits affect the work done when formatting the log output?
Analyze the time complexity of the following git command.
git log --pretty=format:"%h - %an, %ar : %s"
This command lists commit history with a custom format showing short hash, author name, relative date, and commit message.
Look for repeated actions in the command.
- Primary operation: Formatting each commit's data into the specified string.
- How many times: Once for every commit in the history being shown.
As the number of commits increases, the command formats each commit one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 formatting operations |
| 100 | 100 formatting operations |
| 1000 | 1000 formatting operations |
Pattern observation: The work grows directly with the number of commits; doubling commits doubles the work.
Time Complexity: O(n)
This means the time to format the log grows linearly with the number of commits shown.
[X] Wrong: "Formatting the log output is instant no matter how many commits there are."
[OK] Correct: Each commit must be processed and formatted, so more commits mean more work and more time.
Understanding how commands scale with input size helps you write efficient scripts and troubleshoot performance in real projects.
"What if we add a filter to show only commits by one author? How would the time complexity change?"
Practice
%h placeholder represent in git log --pretty=format:"%h %s"?Solution
Step 1: Understand
The placeholder%hin git log format%hstands for the abbreviated commit hash, a short version of the commit ID.Step 2: Compare with other placeholders
%anis author name,%cdis commit date, and%sis the commit message. So%his unique for the commit hash.Final Answer:
Abbreviated commit hash -> Option BQuick Check:
Commit hash short form [OK]
- Confusing %h with %an (author name)
- Thinking %h shows full commit message
- Mixing %h with commit date placeholder
git log --pretty=format?Solution
Step 1: Identify correct placeholders for author and message
The correct placeholders are%anfor author name and%sfor commit message.Step 2: Check syntax correctness
git log --pretty=format:"%an %s" uses%an %swhich is valid. Options A and C use invalid placeholders. git log --pretty=format:"%s %an" reverses order but is still valid syntax, but question asks for author then message.Final Answer:
git log --pretty=format:"%an %s" -> Option AQuick Check:
Author then message [OK]
- Using invalid placeholders like %author or %message
- Swapping order when question specifies author first
- Missing quotes around format string
abc1234, author is Jane, and message is Fix bug?
git log -1 --pretty=format:"%h - %an: %s"
Solution
Step 1: Understand placeholders in format string
The format string is%h - %an: %s. %h is abbreviated hash, %an is author name, %s is commit message.Step 2: Substitute given values
Replacing placeholders: %h = abc1234, %an = Jane, %s = Fix bug. So output is "abc1234 - Jane: Fix bug".Final Answer:
abc1234 - Jane: Fix bug -> Option DQuick Check:
Format placeholders replaced correctly [OK]
- Mixing order of placeholders and values
- Ignoring separators like '-' and ':'
- Confusing full hash with abbreviated hash
git log --pretty=format:"%h %an %msg" but get an error. What is the problem?Solution
Step 1: Identify invalid placeholder
The placeholder%msgdoes not exist in git log formatting. The correct placeholder for commit message is%s.Step 2: Check other parts of command
Quotes are present, and using %h and %an together is valid. The --oneline option is optional and unrelated to this error.Final Answer:
The placeholder %msg is invalid -> Option AQuick Check:
Invalid placeholder causes error [OK]
- Typing %msg instead of %s
- Forgetting quotes around format string
- Assuming --oneline is required for formatting
Solution
Step 1: Identify placeholders for required info
%h is abbreviated hash, %ae is author email, %cd is commit date.Step 2: Choose correct date format option
--date=iso shows date in ISO 8601 format, which matches requirement.Step 3: Verify options
git log --pretty=format:"%h %ae %cd" --date=iso matches all requirements. git log --pretty=format:"%h %an %cd" --date=short uses %an (author name) and short date, not email or ISO. git log --pretty=format:"%H %ae %cd" --date=iso uses %H (full hash), not abbreviated. git log --pretty=format:"%h %ae %cd" --date=relative uses relative date format, not ISO.Final Answer:
git log --pretty=format:"%h %ae %cd" --date=iso -> Option CQuick Check:
Abbreviated hash, email, ISO date [OK]
- Using %an instead of %ae for email
- Choosing wrong date format option
- Using full hash %H instead of abbreviated %h
