Git: Show the initial (root) commit

Thistle help you get to the root of things.

To show the initial, or root, commit* of your repository, use:

$ git log --max-parents=0

(*There may be multiple, as we’ll see.)

For example, on the Django repository:

$ git log --max-parents=0
commit d6ded0e91bcdd2a8f7a221f6a5552a33fe545359
Author: Jacob Kaplan-Moss <...>
Date:   Wed Jul 13 16:56:05 2005 +0000

    Created basic repository structure

    git-svn-id: http://code.djangoproject.com/svn/django/trunk@1 bcc190cf-cafb-0310-a4f2-bffc1f526a37

Add other git log formatting options as necessary, such as -p (--patch) to show the diff. --format='%H' will output only commit SHAs, useful for scripting.

--max-parents=0 restricts the log to root commits which have no previous history. “Vanilla” commits have one parent, the previous commit. Merge commits have two or more, from the branches they merge in.

A repository may actually have multiple root commits. Root commits can be created at any time by starting a new disconnected branch with git switch --orphan. Those commits will appear on the log if their branches are ever merged into the main branch.

Git’s own repository has seven root commits:

$ git log --max-parents=0 --oneline
0ca71b3737 basic options parsing and whatnot.
16d6b8ab6f Initial import of a python script to import changesets from Perforce into git.
cb07fc2a29 git-gui: Initial revision.
161332a521 first working version
1db95b00a2 Add initial version of gitk to the CVS repository
2744b2344d Start of early patch applicator tools for git.
e83c516331 Initial revision of "git", the information manager from hell

The last one listed is Linus Torvalds creating Git in 2005. The others are root commits for tools like git-gui or git-subtree, which started in separate repositories but were merged in whilst preserving their histories.

If you have multpile root commits, restrict the log to the one from the current branch by adding --first-parent:

$ git log --max-parents=0 --first-parent
commit e83c5163316f89bfbde7d9ab23ca2e25604af290
Author: Linus Torvalds <...>
Date:   Thu Apr 7 15:13:13 2005 -0700

    Initial revision of "git", the information manager from hell

Fin

I hope you get to the root of your investigation,

—Adam


Read my book Boost Your Git DX for many more Git lessons.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: