Sub topic 1
What a commit records: author, committer, date and message
Prerequisites
This chapter assumes you can do the following:
- Clone a repository, make commits, and create branches.
- Read basic
git logoutput. - Run commands in a terminal.
Later chapters use regular expressions. This chapter does not.
Why start with what a commit stores
git blame and git log -S do not read your mind. They read the metadata that each commit stores. If you know what that metadata contains and where it can be wrong, you can judge how far to trust their answers.
The fields of a commit
Every commit stores these fields:
- Tree. A snapshot of the whole project at that moment. Git stores snapshots, not diffs. Tools compute diffs by comparing a commit with its parent.
- Parents. Zero, one, or more commits that came directly before it. A normal commit has one parent. A merge commit has two or more. The first commit in a repository has none.
- Author. The name, email, and date of the person who originally wrote the change.
- Committer. The name, email, and date of the person who created this commit object.
- Message. A short summary line, then an optional longer body.
You can see all of them with git cat-file:
git cat-file -p HEADTypical output looks like this:
tree 9f3c1e2a7b5d4c8e0f1a2b3c4d5e6f7a8b9c0d1e
parent 4a1b2c3d4e5f60718293a4b5c6d7e8f901234567
author Maria Lopez <[email protected]> 1764860400 +0000
committer Sam Chen <[email protected]> 1764946800 +0000
Fix timeout handling in parse_config
The default timeout was ignored when the file had no [net] section.Here the author and committer differ, and so do their timestamps. The next section explains why.
Author versus committer
The author is the person who wrote the change. The committer is the person who recorded it in this particular commit. In most commits they are the same person. They differ when a commit is rewritten or applied by someone else. Common cases:
git rebasecreates new commits. The author and author date stay the same. The committer becomes you, and the committer date becomes the time of the rebase.git cherry-pickandgit commit --amendbehave the same way.git amapplies a patch received by email. The author is the patch writer, and the committer is the maintainer who applied it.- A web interface may set the committer to a service account when it merges or squashes.
The two roles matter when you investigate. git blame shows the author by default, so it tells you who wrote a line. It does not tell you who put it into the branch you are looking at.
Two dates
Each commit therefore carries two dates. The author date is when the change was first written. The committer date is when this commit object was created. After a rebase the author date can be months older than the committer date.
git log shows the author date by default. To see both, use a custom format:
git log -3 --format='%h | author: %an %ad | committer: %cn %cd | %s'The placeholders are %h for the abbreviated hash, %an and %cn for author and committer names, %ad and %cd for their dates, and %s for the subject line.
The commit hash
Each commit has an ID, the hash, computed from all of its contents: the tree, parents, author, committer, and message. Changing any field produces a different hash. This is why a rebase gives every rewritten commit a new hash even when the code change is identical. Blame output points at hashes, so a hash you saw before a rebase may no longer exist on the branch afterward.
The message is evidence
The message is often the only record of why a change was made. The subject line summarizes it. The body can explain the reason, link a ticket, or note a trade-off. When you track down a suspicious line later in this tutorial, you will read the message of the commit you find. A message like "fix" tells you little. A message that explains the bug tells you whether the line is safe to change.
To read one commit in full, use:
git show abc1234This prints the author, date, message, and the diff against the parent.
What Git does not verify
Git trusts what you configure. The author name and email come from user.name and user.email, and anyone can set them to any value.
git config user.name
git config user.emailTreat author names as a strong hint, not proof of identity. Signed commits (git log --show-signature) can prove who created a commit, but most repositories do not require them.
Exercise
Exercise
A teammate rebases your feature branch onto main and pushes it. For the rewritten commits, which fields keep their original values, and which change?
Show answer
The author name, author email, author date, and message stay the same. The committer becomes the teammate and the committer date becomes the time of the rebase. The parent changes, so the hash is new as well.
Exercise
Which command prints the abbreviated hash, author name, author date, committer name, and committer date for the last five commits?
Show answer
git log -5 --format='%h %an %ad %cn %cd'