Skip to content

How to Reference the Initial Git Commit

In some cases, you need to know the initial commit hash in a script. While Git uses HEAD to reference the latest commit, it does not have a direct reference to the first commit. Here, we will explain how to get the initial commit hash.

Referencing the initial commit

To get the initial commit hash in your script, use git rev-list instead of git log:

git rev-list

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

INFO

A repository can have more than one root commit. This typically occurs when merging separate projects or using subtree merges.

The git log Command

While git log is used to view and filter commit history, it does not directly output the initial commit hash without additional flags. For scripting purposes, git rev-list is more efficient and reliable.

Dual-run preview — compare with live Symfony routes.