What Is a Commit Object in Git?

Find out what a git commit object is

Internally, git stores information about the project directory tree, commits, etc. as objects in the .git/objects folder. It uses a unique key/hash to refer to these "data objects". One such data object is the "commit object", which (as the name would suggest) contains information about a git commit. A commit object is created when you run the git commit command.

For example, you can use the git cat-file -p <object-hash> command to see the contents of a commit object, like so:

git cat-file -p 95e67358676574c9254f734f5b3f0fc3f43749ed

This would result in an output like the following:

tree 6418daa1541f090b7ab144f09b79d93bb9a60218
parent 81c948a6488fc505be7eb22575a15e2db65c5d32
author John Doe <[email protected]> 1639416517 +0100
committer John Doe <[email protected]> 1639416517 +0100

Related commit message is shown here

As you can see, it contains the following information related to a commit:

Reference to the Tree Object

The commit object stores the hash of the tree object (that represents a snapshot of the contents of the working directory at a certain point in time). This is so that the changes can be preserved and referenced to, whenever needed.

Similar to the commit object, you can use the git cat-file command to see the contents of the tree object, for example, like so:

git cat-file -p 6418daa1541f090b7ab144f09b79d93bb9a60218

It will produce an output similar to the following:

040000 tree 43ceb55a59da70d655153c156fbdcdbd1b9ee751	.github
100644 blob 9187d9c929f5fb86d8f20b1ffe03d6ece8dc79fe	.gitignore
100644 blob e939c09a29d48f98c3a233f7b08ad65ee3f6c889	README.md
040000 tree b0d4a6557ac193f35307a77ee4a6ed14bebbe028	src
# ...

Commit Hash of Parent(s)

In git, when a commit object is created (using the git commit command), value of the reference HEAD points to is set as the "parent" of that commit. It is possible for a commit to have an arbitrary number of parents — in which case, the hash of each parent is stored in the commit object, for example, like so:

# ...
parent 81c948a6488fc505be7eb22575a15e2db65c5d32
parent bdb425d2e0f76f30991f9c92c33fdfb9b09918ff
# ...

This happens when you git merge one or more commits into your current branch (without fast-forwarding).

There can also be a case when a commit has no parent. This happens when you create a new repository and make the first commit. This is called a "root" commit.

Commit Author

Information about the authorship of the commit is stored in the commit object. This includes; the name and email of the person responsible for the change, as well as the date the commit was created. For example, it looks something like the following:

author John Doe <[email protected]> 1639416517 +0100

Committer

Typically, the author of the code is the committer as well:

author John Doe <[email protected]> 1639416517 +0100
committer John Doe <[email protected]> 1639416517 +0100

However, in certain cases, the author and committer could be different people. For example, this could be the case in larger projects, where the setup might be something like the following:

  • Author of the code creates a patch (e.g. using git format-patch) and emails the patch via email (e.g. using git send-email);
  • The committer could be a project maintainer for example, who applies the patch (e.g. using git apply or git am).

Commit Message

This is self-explanatory — it is the message/comment associated with the commit. This is used to describe what the commit is about.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.