Git log
First commit
Version control
Git command
Software development

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.

Browse interview questions

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.

bash
git log --reverse --oneline HEAD | head -n 1

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.

bash
git log --reverse HEAD

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.

bash
git rev-list --max-parents=0 HEAD

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.

bash
git show $(git rev-list --max-parents=0 HEAD)

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:

bash
git rev-list --max-parents=0 --all

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.

bash
git log --reverse --format='%H' HEAD | head -n 1
git log --reverse --format='%s' HEAD | head -n 1

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:

bash
git log --reverse --oneline HEAD | head -n 1

Show the full details of the root commit reachable from the current branch:

bash
git show $(git rev-list --max-parents=0 HEAD | head -n 1)

List all root commits in the whole repository:

bash
git rev-list --max-parents=0 --all

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 --reverse shows commits from oldest to newest.'
  • 'git log --reverse --oneline HEAD | head -n 1 is the simplest interactive answer.'
  • 'git rev-list --max-parents=0 HEAD asks 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 --all if you need root commits across the whole repository.
  • Prefer git rev-list over parsing log output in scripts.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.