How to show the first commit by 'git log'?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Git shows the newest commits first, so finding the very first commit takes either reversing the log or asking Git for root commits explicitly. The right command depends on what you mean by "first": the oldest commit reachable from the current branch, or any root commit that exists anywhere in the repository.
Use git log --reverse
The simplest approach is to reverse the normal log order and limit the output.
This prints the oldest commit reachable from HEAD. If your repository has a normal linear origin, that is often exactly what you want.
You can also view the full commit details instead of the one-line form.
The first entry in that output is the oldest reachable commit.
This approach is easy to remember, but it does more work than necessary if you only care about the root commit hash.
Ask Git For Root Commits Directly
Git can list commits with no parents by using --max-parents=0.
That command is often the best answer because it asks for root commits explicitly instead of reversing the whole history.
If you want to show the commit contents immediately, pass the result to git show.
If there is only one root commit, this prints it directly. If multiple root commits exist, git show will display each one.
Why Multiple Root Commits Can Exist
Many repositories have exactly one root commit, but Git does not require that. Multiple root commits can appear after history imports, unrelated histories, or special repository construction.
To inspect all root commits across all refs, use:
That is different from HEAD, which only considers history reachable from the current branch.
If your question is "what is the first commit in this branch," use HEAD. If your question is "what are the root commits in this repository," use --all.
Showing Only The Hash Or Only The Message
Sometimes you need only one specific field.
The first command prints only the full hash. The second prints only the subject line.
For scripts, git rev-list --max-parents=0 HEAD is usually cleaner than parsing human-oriented log output.
Practical Examples
Show the oldest commit on the current branch in one line:
Show the full details of the root commit reachable from the current branch:
List all root commits in the whole repository:
These commands cover most interactive and scripting use cases.
Common Pitfalls
A common mistake is assuming the oldest commit on HEAD is always the only root commit in the repository. That is true in many repos, but not all.
Another mistake is using plain git log and scrolling manually through a long history. It works, but it is inefficient and easy to get wrong in repositories with merges or multiple histories.
Developers also sometimes parse git log output in scripts when git rev-list is the better plumbing command. If the goal is automation, prefer the command that returns only the needed commit IDs.
Finally, be clear about scope. HEAD means commits reachable from the current branch. --all means all refs Git currently knows about.
Summary
- '
git log --reverseshows commits from oldest to newest.' - '
git log --reverse --oneline HEAD | head -n 1is the simplest interactive answer.' - '
git rev-list --max-parents=0 HEADasks Git directly for root commits.' - Use
git show $(git rev-list --max-parents=0 HEAD | head -n 1)to inspect the first commit fully. - Use
--allif you need root commits across the whole repository. - Prefer
git rev-listover parsing log output in scripts.
Related reading
- How to show uncommitted changes in Git and some Git diffs in detail
- How to shrink the .git folder
- How to solve Permission denied publickey error when using Git?
- How to sort in-place using the merge sort algorithm?
- How to sparsely checkout only one single file from a git repository?
- How to specify the private SSH-key to use when executing shell command on Git?
- How to squash all commits on branch
- How to squash all commits on branch
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.