Viewing commit history with git log - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use git log, we want to see past changes in a project. Understanding how long this takes helps us know if it will slow down as the project grows.
We ask: How does the time to show commit history change when there are more commits?
Analyze the time complexity of the following code snippet.
git log --oneline
This command lists all commits in a simple, one-line format, showing the commit ID and message for each commit.
- Primary operation: Reading and displaying each commit in the history.
- How many times: Once for every commit in the repository.
As the number of commits grows, the time to list them grows too, because each commit is shown one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 commits read and shown |
| 100 | About 100 commits read and shown |
| 1000 | About 1000 commits read and shown |
Pattern observation: The work grows directly with the number of commits; double the commits means double the work.
Time Complexity: O(n)
This means the time to show commit history grows in a straight line with the number of commits.
[X] Wrong: "Showing commit history takes the same time no matter how many commits there are."
[OK] Correct: Each commit must be read and displayed, so more commits mean more work and more time.
Knowing how commands like git log scale helps you understand performance in real projects. It shows you how tools handle growing data, a useful skill in many jobs.
"What if we add a filter to show only the last 10 commits? How would the time complexity change?"
Practice
git log command do in a Git repository?Solution
Step 1: Understand the purpose of
Thegit loggit logcommand is used to view the commit history in a Git repository.Step 2: Compare with other Git commands
Other commands likegit statusshow file status, andgit branchmanages branches, butgit logspecifically shows commits.Final Answer:
Shows the history of commits made in the repository -> Option BQuick Check:
git log = commit history [OK]
- Confusing git log with git status
- Thinking git log deletes commits
- Mixing git log with branch creation
Solution
Step 1: Identify the option for one-line commit summary
The--onelineoption condenses each commit to a single line showing the commit hash and message.Step 2: Verify other options
Options like--summary,--details, and--shortare not valid git log options for this purpose.Final Answer:
git log --oneline -> Option AQuick Check:
--oneline = short commit list [OK]
- Using non-existent options like --summary
- Confusing --oneline with --short
- Forgetting the double dash before options
git log -2 --oneline, what will be the output?Solution
Step 1: Understand the
The-2option-2option limits the output to the last two commits only.Step 2: Understand the
The--onelineoption--onelineoption shows each commit in a single line summary.Final Answer:
Shows the last two commits each summarized in one line -> Option CQuick Check:
-2 + --oneline = last two commits short [OK]
- Thinking -2 shows first two commits
- Expecting detailed multi-line output with --oneline
- Confusing exclusion of commits with limiting output
git log --oneline -p but got an error. What is the likely cause?Solution
Step 1: Understand the options
--onelineand-p--onelineshows a brief summary, while-pshows patch (diff) details.Step 2: Check compatibility of options
These two options conflict because one shows a short summary and the other shows detailed changes, so Git throws an error.Final Answer:
The options --oneline and -p cannot be used together -> Option AQuick Check:
--oneline + -p conflict = error [OK]
- Assuming branch name is required for git log
- Thinking git fetch fixes git log errors
- Ignoring that empty repo causes no output, not error
Solution
Step 1: Understand the requirement for custom format
You want author, date, and message in one line, so a custom format with--pretty=format:is needed.Step 2: Analyze the format string
%his short commit hash,%anis author name,%aris relative date, and%sis commit message. This matches the requirement.Step 3: Check other options
--oneline --author-date-orderdoes not show author or date explicitly.-p --statshows diffs and stats, not compact.--graph --decorateshows branch graph and refs, not author/date/message in one line.Final Answer:
git log --pretty=format:"%h - %an, %ar : %s" -> Option DQuick Check:
Custom format = author, date, message [OK]
- Using --oneline without author or date info
- Expecting -p to show summary info
- Confusing --graph with formatting output
