Sub topic 2
Your first git log
This chapter assumes you can create a repository, stage files, commit, and switch branches. It also assumes you are comfortable in a terminal. Here you will run git log with no options and learn to read what it prints.
What git log does
git log starts at the commit that HEAD points to. HEAD is the pointer to your current position, usually the tip of your checked-out branch. From there it follows each commit's parent link backward and prints every commit it reaches. This is why the newest commit appears first.
Running it
git logA repository with three commits prints something like this:
commit 8c4d9e2a1b7f3d605e9a2c4b81f0d37a6e5c1b94 (HEAD -> main)
Author: Alice Example <[email protected]>
Date: Thu Dec 3 10:14:52 2026 +0000
Validate config after loading
commit 3f2a1b0c9d8e7f6a5b4c3d2e1f0a9b8c7d6e5f4a
Author: Bob Example <[email protected]>
Date: Tue Dec 1 16:40:05 2026 +0000
Add config loader
commit a91c07e4d2b6385f0c1e9a7d4b2368f5c0e1d7a3
Author: Alice Example <[email protected]>
Date: Mon Nov 30 09:02:31 2026 +0000
Initial commitEach entry has the same parts:
| Part | Meaning |
|---|---|
| `commit` line | The full 40-character hash that identifies the commit. Names such as `HEAD -> main` appear in parentheses when a ref points here. |
| `Author` | The name and email recorded when the change was written. |
| `Date` | When the author created the commit, in the author's time zone offset. |
| Message | The text you gave to `git commit`, indented by four spaces. |
The author is not always the person who committed. Rebasing or applying a patch keeps the original author but changes the committer. The default output shows only the author.
Merge commits
A merge commit has two parents, so its entry gains an extra line:
commit 5d0e7b3c...
Merge: 3f2a1b0 9b8c7d6
Author: Alice Example <[email protected]>The Merge: line lists the abbreviated hashes of the parents. Both histories are walked, so commits from the merged branch appear in the output too.
Moving through the output
When the output is longer than your screen, Git sends it to a pager, which is usually less. These keys work in less:
- Press Space or
fto go forward a page. - Press
bto go back a page. - Press
/textto search, thennfor the next match. - Press
qto quit.
To limit how much history is printed, use -n:
git log -n 2This prints the two most recent commits. Later chapters cover more ways to narrow the output.
Common surprises
- A new repository prints an error. Before the first commit,
git logfails with "your current branch 'main' does not have any commits yet". This is expected. - A commit you made is missing.
git logshows only commits reachable fromHEAD. A commit on another branch will not appear until you switch to that branch or rungit log --all. - The order does not match the dates. Git orders commits by walking the graph, not by sorting on the date field. Rebased or imported commits can have dates that look out of order.
- The terminal seems stuck. It is probably waiting for you in the pager. Press
q.
Exercise
You run git log on branch feature, and a commit you made yesterday on main is not listed. Why, and what option would show it?
Show answer
git log follows parent links only from HEAD, which is on feature. If the main commit is not an ancestor of feature, it is not reachable and is not printed. git log --all starts from every branch and tag, so it would include the commit.