Viewing the version history¶
At this point the homelab repository has four commits. This page shows how Git stores them and how to explore them.
The commit structure¶
A Git commit is made of three kinds of objects:
| Object | Role |
|---|---|
| Commit | Contains the metadata: author, log message, commit time. It also points to a tree and to its parent commit. |
| Tree | Tracks the names and locations of files and directories in the repository. It works like a dictionary that maps names (keys) to files or subdirectories. |
| Blob | Binary Large OBject. It may contain data of any kind; for Git, it is a compressed snapshot of one file's contents. |
In short: the commit says who, when, and why, the tree says which files exist and where, and each blob holds what a file contains. Each commit also records its parent, the commit that came just before it: this is how Git chains commits into a history (Pro Git, Git Objects).
Visualizing the commit structure¶
Here are the first three commits of the homelab repository, with the real identifiers of their objects:
| Commit | What changed |
|---|---|
2553c7d Create README and inventory |
README.md and inventory.csv created |
dd032b5 Add NAS and services list |
inventory.csv modified, services.md created |
cdf805a Add web server and backup reminder |
README.md and inventory.csv modified |
flowchart TB
c1["commit 2553c7d<br/>Create README and inventory"]
c2["commit dd032b5<br/>Add NAS and services list"]
c3["commit cdf805a<br/>Add web server and backup reminder"]
c1 --> t1["tree 732ad89"]
c2 --> t2["tree d8629b9"]
c3 --> t3["tree b4dfdde"]
t1 --> bA(["README.md<br/>blob 95c56b7"])
t1 --> bB(["inventory.csv<br/>blob 1fb1eb9"])
t2 --> bA
t2 --> bC(["inventory.csv<br/>blob 59c3965"])
t2 --> bD(["services.md<br/>blob 6f0ce8d"])
t3 --> bE(["README.md<br/>blob 7ad4d7a"])
t3 --> bF(["inventory.csv<br/>blob a695d1c"])
t3 --> bD
What the diagram shows:
- Each commit has its own tree that lists every file in the project at that moment.
- When a file changes, the new commit's tree points to a new blob with the new content.
inventory.csvchanges in every commit, so it has a new blob each time. - When a file is unchanged, the tree simply points to the existing blob.
README.mddid not change indd032b5, so both trees share blob95c56b7.services.mddid not change incdf805a, so it reuses blob6f0ce8d. Git does not store a second copy of identical content.
This is why each commit is a full snapshot of the project, yet the repository does not grow with a complete copy of every file at each commit.
You can inspect these objects yourself in any repository with git cat-file -p:
$ git cat-file -p HEAD
tree bb4bd28d657a00eddaf316c7866af6d3255defa2
parent cdf805a8026b07299704613a1e9b1ed174b0fe7f
author Sam Rivera <[email protected]> 1775165400 +0000
committer Sam Rivera <[email protected]> 1775165400 +0000
Add media server
$ git cat-file -p 'HEAD^{tree}'
100644 blob 7ad4d7ae4fbec3feed9c5cd2c38f637868e91cb8 README.md
100644 blob 00b6c383f0baee35d8b4c3b39f3a173be7fa9d3d inventory.csv
100644 blob 6f0ce8ddc9638812e0738acad9ed191026d66af6 services.md
The Git hash¶
Every commit, and every tree and blob, is identified by a hash, for example:
420feb7935469445a05b69843c384e04fed9f255
A hash is computed by a hash function from the content of the object. By default, Git uses SHA-1, which produces 40 hexadecimal characters; repositories can also be created with SHA-256 (git init --object-format).
A hash looks random, but it is deterministic: the same content always produces the same hash. This is what makes it efficient to share and store data:
- if two files are the same, their hashes are the same, which is exactly how the trees above can share blobs;
- so Git only needs to compare hashes, not entire files, to know whether content has changed or is already present.
Git log¶
git log shows the commit history, from newest to oldest:
$ git log
commit 420feb7935469445a05b69843c384e04fed9f255
Author: Sam Rivera <[email protected]>
Date: Thu Apr 2 21:30:00 2026 +0000
Add media server
commit cdf805a8026b07299704613a1e9b1ed174b0fe7f
Author: Sam Rivera <[email protected]>
Date: Mon Mar 16 20:05:00 2026 +0000
Add web server and backup reminder
commit dd032b589c394eedb969ed235b4faec8efcc65dd
Author: Sam Rivera <[email protected]>
Date: Mon Mar 9 18:40:00 2026 +0000
Add NAS and services list
:
Each entry contains the full commit hash, the author (name and email), the date, and the log message.
When the history is longer than the screen, Git displays it in a pager (usually less), shown by the : prompt at the bottom:
| Key | Action |
|---|---|
Space |
Show the next page, further down the list, which contains older commits |
q |
Quit the log and return to the terminal |
Version history tips and tricks¶
As a project grows, it gets more commits, and the output of git log grows with it. These options narrow the output.
Restricting the number of commits¶
Add - followed by a number to show only the most recent commits:
git log -3 # only the 3 most recent commits
-3 is a short form of -n 3 or --max-count=3.
Restricting to one file¶
Add a file path to see only the commits that changed this file:
git log README.md
The path is relative to your current directory.
Combining techniques¶
Options and paths can be combined. Here, the two most recent commits that changed the inventory:
$ git log -2 inventory.csv
commit 420feb7935469445a05b69843c384e04fed9f255
Author: Sam Rivera <[email protected]>
Date: Thu Apr 2 21:30:00 2026 +0000
Add media server
commit cdf805a8026b07299704613a1e9b1ed174b0fe7f
Author: Sam Rivera <[email protected]>
Date: Mon Mar 16 20:05:00 2026 +0000
Add web server and backup reminder
One line per commit¶
git log --oneline prints each commit on a single line, with its short hash. It is the quickest way to get an overview, or to copy a hash:
$ git log --oneline
420feb7 Add media server
cdf805a Add web server and backup reminder
dd032b5 Add NAS and services list
2553c7d Create README and inventory
Customizing the date range¶
Restrict git log to a period with --since and --until:
$ git log --oneline --since='2026-03-05 00:00' --until='2026-03-20 23:59'
cdf805a Add web server and backup reminder
dd032b5 Add NAS and services list
--after and --before are synonyms of --since and --until (git log documentation).
Accepted date formats¶
| Natural language | Dates |
|---|---|
"2 weeks ago" |
"2026-03-15": ISO 8601 YYYY-MM-DD, recommended |
"3 months ago" |
"15 Mar 2026" or "15 March 2026" |
"yesterday" |
"Mar 15 2026" |
Prefer the ISO 8601 format YYYY-MM-DD: it cannot be misread. A numeric date such as 12-06-2026 could mean 6 December or 12 June, and Git reads it month first (6 December).
A date without a time
A date without a time, such as --since='2026-03-05', uses the current time of day on that date. To include the whole day, add the time explicitly: --since='2026-03-05 00:00' and --until='2026-03-20 23:59', as in the example above.
Finding a particular commit¶
To inspect one commit, find its hash with git log, then pass it to git show. You do not need the full 40 characters: the first 8 to 10 are enough.
$ git show 420feb79
commit 420feb7935469445a05b69843c384e04fed9f255
Author: Sam Rivera <[email protected]>
Date: Thu Apr 2 21:30:00 2026 +0000
Add media server
diff --git a/inventory.csv b/inventory.csv
index a695d1c..00b6c38 100644
--- a/inventory.csv
+++ b/inventory.csv
@@ -2,3 +2,4 @@ hostname,ip,role
pi-dns,192.168.1.10,dns
nas,192.168.1.20,storage
pi-web,192.168.1.30,web
+192.168.1.40,media,mediabox
The output has two parts:
- Log: the same information as in
git log(hash, author, date, message). - Diff: the changes introduced by this commit. The line starting with
+was added. Here it reveals a data entry error: the columns are in the wrong order (192.168.1.40,media,mediaboxinstead ofmediabox,192.168.1.40,media). This is howgit showhelps find the commit that introduced a problem. How to read a diff in detail is explained in Comparing versions.
How short can a hash be?
Git accepts any leading part of a hash, as long as it is unique in the repository (gitrevisions). Git itself displays short hashes of at least 7 characters, as in git log --oneline. Using 8 to 10 characters keeps the prefix unique even in large repositories.
Hands-on labs¶
Three labs, from guided to more autonomous. Each one has its own setup, so you can do them in any order and repeat them as often as you like. Type the commands rather than pasting them, and read every output before moving on: the goal is to build reflexes, not to reach the end.
Lab 1: investigate a project's history¶
Objective: use git log and git show options to answer questions about an existing history.
Prerequisites and initial state: Git installed. The setup script builds a practice repository in ~/git-practice/history with five commits at fixed dates in 2026.
Setup (copy the whole block):
mkdir -p ~/git-practice/history && cd ~/git-practice/history
git init -q -b main
git config user.name "Practice User"
git config user.email "[email protected]"
c() { GIT_AUTHOR_DATE="$1" GIT_COMMITTER_DATE="$1" git commit -q -m "$2"; }
mkdir docs
echo "# Homelab" > README.md
echo "hostname,ip,role" > docs/inventory.csv
git add . && c "2026-02-23T10:00:00" "Create README and inventory"
echo "pi-dns,192.168.1.10,dns" >> docs/inventory.csv
git add . && c "2026-03-03T10:00:00" "Add DNS server"
echo "TODO: schedule weekly backups." >> README.md
git add . && c "2026-03-09T10:00:00" "Add backup reminder"
echo "nas,192.168.1.20,storage" >> docs/inventory.csv
git add . && c "2026-03-17T10:00:00" "Add NAS"
echo "192.168.1.40,media,mediabox" >> docs/inventory.csv
git add . && c "2026-04-02T10:00:00" "Add media server"
unset -f c
Tasks:
- Display the full history. How many commits are there, and which one is the most recent?
- Show only the 2 most recent commits.
- Show only the commits that changed
README.md. - From inside the
docsdirectory, show the 2 most recent commits that changedinventory.csv. - Back in the repository root, list the commits made between 1 and 10 March 2026 (inclusive).
- Display the content of the commit
Add media serverusing its hash, and find the data entry error it introduced.
Expected result and verification:
- 5 commits; the newest is
Add media server. Add media serverandAdd NAS.Add backup reminderandCreate README and inventory.Add media serverandAdd NAS.Add backup reminderandAdd DNS server.- The diff shows
+192.168.1.40,media,mediabox: the columns do not follow thehostname,ip,roleorder.
Solution
# 1. Full history (press q to quit if a pager opens)
git log
# 2. Two most recent commits
git log -2
# 3. Commits that changed README.md
git log README.md
# 4. Combine a limit and a path, from the docs directory
cd docs
git log -2 inventory.csv
cd ..
# 5. Date range, with explicit times to include both full days
git log --since='2026-03-01 00:00' --until='2026-03-10 23:59'
# 6. Find the hash, then show the commit
git log -1 --oneline # e.g. 1a2b3c4 Add media server
git show 1a2b3c4 # replace with your own hash
-2and a file path can be combined in the same command. The path in step 4 is relative todocs/.- In step 5,
00:00and23:59make the range cover both full days, whatever the current time. git showprints the log entry, then the diff. The added line+192.168.1.40,media,mediaboxbreaks the column order of the file.- Your hashes differ from the example, because they depend on the author and the dates.
Clean up when you are done: rm -rf ~/git-practice/history.
Lab 2: write a history, then read it¶
Objective: build a short history yourself, then navigate it with short hashes.
Prerequisites and initial state: Git installed. The setup creates an empty repository in ~/git-practice/my-history.
Setup:
mkdir -p ~/git-practice/my-history && cd ~/git-practice/my-history
git init -q -b main
git config user.name "Practice User"
git config user.email "[email protected]"
Tasks:
- Create a script
backup.shcontaining the line#!/bin/sh, and commit it with the messageCreate backup script. - Add the line
rsync -a /srv/data /mnt/backupto the script, and commit with the messageCopy data to the backup disk. - Add the line
echo "Backup done", and commit with the messagePrint a message when done. - Display the history, one line per commit.
- Show the second commit (
Copy data to the backup disk) using its 7-character short hash. Try again with only the first 4 characters, then with only the first 2. - Display the full log entry of the most recent commit only, without its diff.
Expected result and verification:
- Task 4 shows three lines, newest first:
Print a message when done,Copy data to the backup disk,Create backup script. - Task 5: with 7 and 4 characters,
git showprints the commit and a diff adding+rsync -a /srv/data /mnt/backup. With 2 characters, it fails withfatal: ambiguous argument '..': unknown revision or path not in the working tree. - Task 6 shows the author
Practice User, today's date, and the messagePrint a message when done.
Solution
# 1 to 3. Three commits
echo '#!/bin/sh' > backup.sh
git add backup.sh
git commit -m "Create backup script"
echo "rsync -a /srv/data /mnt/backup" >> backup.sh
git add backup.sh
git commit -m "Copy data to the backup disk"
echo 'echo "Backup done"' >> backup.sh
git add backup.sh
git commit -m "Print a message when done"
# 4. One line per commit
git log --oneline
# 5. Short hashes (replace with your own)
git show 5c1e2a7 # 7 characters
git show 5c1e # 4 characters: still unique here
git show 5c # fatal: ambiguous argument '5c': unknown revision ...
# 6. The latest log entry
git log -1
'#!/bin/sh'is quoted with single quotes, because!has a special meaning for the shell inside double quotes.- Git accepts a hash prefix of at least 4 characters, if it is unique. Below 4, Git does not even try to read it as a hash, so it reports an unknown revision.
git log -1shows the log entry only;git showwould add the diff.
Clean up when you are done: rm -rf ~/git-practice/my-history.
Lab 3: the detective¶
Objective: answer real questions about a project by reading its history: who, when, what, and why.
Prerequisites and initial state: Git installed. The setup builds ~/git-practice/detective: six commits by two people, Sam and Alex, between March and April 2026. One of them introduced an error in the inventory.
Setup (copy the whole block):
mkdir -p ~/git-practice/detective && cd ~/git-practice/detective
git init -q -b main
c() { GIT_AUTHOR_NAME="$1" GIT_AUTHOR_EMAIL="$2@example.com" GIT_COMMITTER_NAME="$1" \
GIT_COMMITTER_EMAIL="$2@example.com" GIT_AUTHOR_DATE="$3" GIT_COMMITTER_DATE="$3" \
git commit -q -m "$4"; }
printf 'hostname,ip,role\npi-dns,192.168.1.10,dns\n' > inventory.csv
printf '# Services\n- pi-dns: Pi-hole\n' > services.md
git add . && c "Sam Rivera" sam "2026-03-02T09:00:00" "Create inventory and services"
echo "nas,192.168.1.20,storage" >> inventory.csv
git add . && c "Sam Rivera" sam "2026-03-10T18:30:00" "Add NAS"
echo "- nas: Samba shares" >> services.md
git add . && c "Alex Martin" alex "2026-03-11T08:15:00" "Document NAS service"
echo "web01,192.168.1.30,web,," >> inventory.csv
git add . && c "Alex Martin" alex "2026-04-01T21:00:00" "Add web server"
sed -i 's/192.168.1.10/192.168.1.11/' inventory.csv
git add . && c "Sam Rivera" sam "2026-04-03T10:00:00" "Update inventory"
echo "- web01: Nginx" >> services.md
git add . && c "Alex Martin" alex "2026-04-05T14:00:00" "Document web service"
unset -f c
Tasks: answer each question with Git commands only, without opening the files.
- How many people worked on the project, and how many commits did each one make?
- The message
Update inventorysays nothing useful. What did this commit actually change? - Which commits changed
services.md, and who wrote them? - What happened between 1 and 4 April 2026, both days included?
web01has two extra commas at the end of its line. Who introduced them, and when?- Propose a better message for the commit of question 2.
Expected result and verification:
- Two people: Sam Rivera and Alex Martin, with three commits each.
- The DNS server's address changed from
192.168.1.10to192.168.1.11. Document web service(Alex),Document NAS service(Alex),Create inventory and services(Sam).Update inventoryandAdd web server.- Alex Martin, on Wed 1 April 2026 at 21:00, in
Add web server: the diff adds+web01,192.168.1.30,web,,. - For example
Move DNS server to 192.168.1.11.
Solution
# 1. Read the Author lines
git log # 3 x Sam Rivera, 3 x Alex Martin
# 2. Show the vague commit
git log --oneline # find the hash of "Update inventory"
git show 1f2e3d4 # -pi-dns,192.168.1.10,dns / +pi-dns,192.168.1.11,dns
# 3. Restrict to one file
git log services.md
# 4. Date range, whole days
git log --oneline --since='2026-04-01 00:00' --until='2026-04-04 23:59'
# 5. Commits that touched the inventory, then the suspect
git log --oneline inventory.csv
git show 9a8b7c6 # hash of "Add web server"
git logandgit showanswer "who" and "when" for free: every commit records its author and date.- "Why" only comes from the message.
Update inventoryforces the reader to open the diff, whileMove DNS server to 192.168.1.11answers the question directly. - Question 5 is the typical bug hunt: list the commits that touched the broken file, then inspect the suspects one by one with
git show. - Your hashes differ from the examples above.
Clean up when you are done: rm -rf ~/git-practice/detective.