How to Reference the Initial Git Commit

In some cases, you want to know the initial commit hash in your script for any reason. As you know, Git has a HEAD, which shows the last commit, but Git doesn't have a TAIL according to this logic. Here, we will explain how to get the initial commit hash.

Watch a course Git & GitHub - The Practical Guide

Referencing the initial commit

To get the initial commit in your script, you must run git-rev-list instead of git log in the following way:

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

Otherwise, you can run the following to get the list of all parentless (root) commits accessible from the current branch:

git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$"
There can exist more than one TAIL root commit in a repository. This can be a result of joining separate projects in one or using subtree merge of an isolated developed subproject.

The git log Command

The git log command is executed to list and filter the project history, examine a repository’s history, and search for particular changes. The git log only works on the committed history, whereas the git status command takes control of the working directory and the index. Log output can be personalized in very different ways, from filtering git commits to displaying them in a user-defined format.