A Git commit, represented by a single building block in my Git mental model, is a frozen moment in time for your project (sometimes referred to as a snapshot). Along with this snapshot Git stores metadata about the commit (e.g. the author of the commit).
Each commit has a unique identifier called the commit hash (a.k.a. SHA), a 40 character long alphanumeric string (e.g. 2b3a38be1df114556a019986dcfbfedda593925f
).
Commit Hash
The commit hash is multiple factors including the snapshot of the files, the author, the time, and all of the commits that came before it.
Even if you try to recreate a commit by making the same changes, you’ll still end up with a different hash.
No man ever steps in the same river twice, for it’s not the same river and he’s not the same man.
Shorter Version of the Commit Hash
Because seven digits is typically enough to uniquely identify a commit, you’ll often see the commit hash truncated to seven characters (e.g. 2b3a38b
).
Parent
One of the pieces of metadata stored with the commit is the parent
of the commit. This is the commit that came directly before it (or directly under it in a stack of blocks). A commit knows what commit came before it but it does NOT know what commit comes after it.
Given a commit we can trace back through the commits that came before it.
This property has an impact on Git branches work.
Multiple Parents
Typically a commit has a single parent with each commit being built on top of the commit that came before it, however a merge commit has multiple parents.
Example
After cloning my example Git branch with blocks repo. I can view my commits with git log --oneline
2b3a38b (HEAD -> main, origin/main) B
ec6a2c7 A
Here I can see commit B
has an (abbreviated) commit hash of 2b3a38b
.
git show
In order to see the meta information and changes introduced by commit B
, I use git show 2b3a38b
.
Note: git show
without a specific branch will display the most recent commit on the branch you’re on.
Author: Sal Ferrarello <sal@example.com>
Date: Sun Jan 24 12:47:40 2021 -0500
B
diff --git a/letters.txt b/letters.txt
index f70f10e..35d242b 100644
--- a/letters.txt
+++ b/letters.txt
@@ -1 +1,2 @@
A
+B
Here I can see that I authored the commit, when I authored it, and the change it introduced (I added the letter B
). This commit only has a single line commit message but if more information were included, it would be displayed here.
Under the Hood of a Git Commit
Some Git commands are good for exposing low level information but are not used in a typical workflow.
git cat-file -p <commit>
is one of these commands.
When I run this, I can see further details of the commit
git cat-file -p 2b3a38b
tree 5214dd562d7670da09324a86b87f6a245eb12bbf
parent ec6a2c79273bba5669b25f7d52dfabe6f0d424e1
author Sal Ferrarello <sal@example.com> 1611510460 -0500
committer Sal Ferrarello <sal@example.com> 1611510460 -0500
B
The primary thing I want to highlight here is the parent value of ec6a2c79273bba5669b25f7d52dfabe6f0d424e1
.
Going back to the output of git log --oneline
2b3a38b (HEAD -> main, origin/main) B
ec6a2c7 A
where can see parent
points to the commit below our commit (ec6a2c7
)
Leave a Reply