Git Log: Viewing History
A ship's captain keeps a logbook. Every entry says what happened, when, and who wrote it down. Months later, anyone can open the book and find out when the ship reached a port or why it changed course.
Every commit you make is an entry in your project's logbook, and git log is how you read it. It answers questions like "what changed this week?", "who added the late fee?" and "when was this file last touched?" without opening a single file.
In this lesson, you will learn to show the history in different levels of detail, to print exactly the fields you want, and to filter the history down to the commits you are looking for.
A Project with Some History
To have something to read, Navika builds a small history with her teammates Srihitha and Advitha. In a real team, each person commits on their own computer. Here, everything happens in one repository, and --author records who wrote each change:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
printf 'Term 1 fee: $12,000\nTerm 2 fee: $12,000\n' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fees"
echo "class FeeCalculator {}" > FeeCalculator.java
git add FeeCalculator.java
git commit --quiet --author="Srihitha Anamaneni <srihitha@example.com>" -m "Add the fee calculator"
printf 'Term 1 fee: $12,000\nTerm 2 fee: $12,500\n' > fees.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Fix the term 2 fee"
echo '// late fee: $250 after the 10th' >> FeeCalculator.java
echo 'Late fee: $250' >> fees.txt
git commit --quiet -a --author="Srihitha Anamaneni <srihitha@example.com>" -m "Add the late fee rule"
echo "Run: java FeeCalculator.java" >> README.md
git commit --quiet -a -m "Explain how to run the app"
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -a --author="Advitha Anamaneni <advitha@example.com>" -m "Add the hostel fee"
lsFeeCalculator.java
README.md
fees.txtls shows the three files. Behind them are seven commits by three authors. Now let's read them.
The Full View
With no options, git log shows every commit, newest first. Here, -2 limits it to the two newest:
git log -2commit bb5be3eccbdb82de6f137f0dd52e03bdcb805633 (HEAD -> main)
Author: Advitha Anamaneni <advitha@example.com>
Date: Mon Jan 5 10:21:00 2026 -0600
Add the hostel fee
commit b2bf1489dcfc848531072d0bfcc904f72916a3a4
Author: Navika Anamaneni <navika@example.com>
Date: Mon Jan 5 10:19:00 2026 -0600
Explain how to run the appEach entry has four parts:
| Part | Meaning |
|---|---|
commit and a long ID | The commit's full ID. (HEAD -> main) marks the commit you are on and the branch that points to it. |
Author: | Who wrote the change, with their email |
Date: | When the change was written |
| The indented text | The full commit message |
When the history is longer than your screen, Git shows it one page at a time. Press Space for the next page, b to go back, and q to quit.
One Line per Commit
Most of the time you only want an overview. --oneline shows each commit as its short ID and subject:
git log --onelinebb5be3e (HEAD -> main) Add the hostel fee
b2bf148 Explain how to run the app
7c6aa3e Add the late fee rule
03b7a0b Fix the term 2 fee
32f03fa Add the fee calculator
fe87780 Add the term fees
dd34d8a Add project READMEThis is the view you will use most. The short IDs are enough to name a commit in any other Git command, as the later lesson "Naming Commits: HEAD, ~ and ^" explains.
Syntax
git log [options] [<commit>] [-- <path>...]| Option | It is used to |
|---|---|
--oneline | Show one line per commit: short ID and subject |
-<n> or -n <n> | Show only the newest n commits |
--stat | List the files each commit changed, with counts |
-p | Show the full changes (the patch) of each commit |
--graph | Draw the history as a graph, useful once branches appear |
--all | Include every branch, not only the current one |
--reverse | Show the oldest commit first |
--format="..." | Print the fields you choose |
Which Files Changed: --stat
--stat adds a short list of the files each commit touched:
git log --stat -2commit bb5be3eccbdb82de6f137f0dd52e03bdcb805633 (HEAD -> main)
Author: Advitha Anamaneni <advitha@example.com>
Date: Mon Jan 5 10:21:00 2026 -0600
Add the hostel fee
fees.txt | 1 +
1 file changed, 1 insertion(+)
commit b2bf1489dcfc848531072d0bfcc904f72916a3a4
Author: Navika Anamaneni <navika@example.com>
Date: Mon Jan 5 10:19:00 2026 -0600
Explain how to run the app
README.md | 1 +
1 file changed, 1 insertion(+)fees.txt | 1 + means one line was added to fees.txt. The + and - signs form a small bar showing how many lines were added and removed. The last line totals the commit.
What Exactly Changed: -p
-p (short for patch) shows every changed line. It is long, so it is usually combined with a limit or a file name:
git log -p -1commit bb5be3eccbdb82de6f137f0dd52e03bdcb805633 (HEAD -> main)
Author: Advitha Anamaneni <advitha@example.com>
Date: Mon Jan 5 10:21:00 2026 -0600
Add the hostel fee
diff --git a/fees.txt b/fees.txt
index cd71fba..5881345 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1,3 +1,4 @@
Term 1 fee: $12,000
Term 2 fee: $12,500
Late fee: $250
+Hostel fee: $9,000The part after the message is a diff: + marks the added line, and the lines without a sign are unchanged lines around it. The lesson "How to Read a Diff" explains every line of this format.
Choose Your Own Format
--format prints exactly the fields you ask for. Each % code is replaced with a value from the commit:
git log --format="%h %ad %an %s" --date=format:"%Y-%m-%d %H:%M"bb5be3e 2026-01-05 10:21 Advitha Anamaneni Add the hostel fee
b2bf148 2026-01-05 10:19 Navika Anamaneni Explain how to run the app
7c6aa3e 2026-01-05 10:17 Srihitha Anamaneni Add the late fee rule
03b7a0b 2026-01-05 10:14 Advitha Anamaneni Fix the term 2 fee
32f03fa 2026-01-05 10:12 Srihitha Anamaneni Add the fee calculator
fe87780 2026-01-05 10:09 Navika Anamaneni Add the term fees
dd34d8a 2026-01-05 10:06 Navika Anamaneni Add project README| Code | Prints |
|---|---|
%H / %h | Full / short commit ID |
%an / %ae | Author name / email |
%ad | Author date, in the style chosen with --date |
%ar | Author date, relative, such as "3 days ago" |
%cn / %cd | Committer name / date |
%s | Subject |
%b | Body |
%d | Branch and tag names, such as (HEAD -> main) |
%n | A new line |
--date=format: takes its own codes for the date: %Y-%m-%d %H:%M is year, month, day, hours and minutes. Ready-made styles include --date=short (YYYY-MM-DD), --date=iso and --date=relative.
You will also see --pretty=format:"...". It does the same, except that it doesn't end the last line with a new line, so --format is the easier choice.
Filter the History
Long histories are searched with filters. Each one keeps only the commits that match, and you can combine them.
By author. --author matches part of the name or email:
git log --oneline --author="Advitha"bb5be3e (HEAD -> main) Add the hostel fee
03b7a0b Fix the term 2 feeBy message. --grep matches text in the commit message. -i ignores upper and lower case:
git log --oneline -i --grep="fee"bb5be3e (HEAD -> main) Add the hostel fee
7c6aa3e Add the late fee rule
03b7a0b Fix the term 2 fee
32f03fa Add the fee calculator
fe87780 Add the term feesBy file. Put the path after -- to see only the commits that changed that file:
git log --oneline -- FeeCalculator.java7c6aa3e Add the late fee rule
32f03fa Add the fee calculatorBy date. --since and --until (also spelled --after and --before) take a date and time. All seven commits were made on one morning, a few minutes apart (look at the times in the custom format above), so this example uses times. Both ends are included:
git log --oneline --since="2026-01-05 10:12" --until="2026-01-05 10:17"7c6aa3e Add the late fee rule
03b7a0b Fix the term 2 fee
32f03fa Add the fee calculatorIn daily work you would write --since="2 weeks ago" or --since=2026-01-01.
Combined. Filters of different kinds must all match. Here: commits by Advitha that changed fees.txt:
git log --oneline --author="Advitha" -- fees.txtbb5be3e (HEAD -> main) Add the hostel fee
03b7a0b Fix the term 2 fee| Filter | Keeps commits |
|---|---|
--author="<text>" | Whose author name or email contains the text |
--grep="<text>" | Whose message contains the text (-i for any case) |
--since=<date> / --until=<date> | Made after / before a date |
-- <path> | That changed the file or folder |
--follow -- <file> | That changed the file, also before it was renamed |
-S "<text>" | That added or removed the text in a file |
Count and Summarize
git shortlog groups commits by author. -s shows only the counts and -n sorts by them. HEAD names the history to count; in a terminal you can leave it out, but in a script git shortlog would wait for input without it:
git shortlog -sn HEAD
git rev-list --count HEAD 3 Navika Anamaneni
2 Advitha Anamaneni
2 Srihitha Anamaneni
7git rev-list --count HEAD prints the total number of commits on the current branch. You will also see git log --oneline | wc -l used for this, which counts the lines of the one-line log.
In Practice
The command line is the quickest way to search history, but a picture helps with a long or branchy one. Git comes with gitk, a simple history browser (run gitk --all). IntelliJ IDEA, VS Code (with its Git Graph or GitLens extensions) and the GitHub, GitLab and Bitbucket websites all have a history view that shows the same information, with clickable commits. Every one of them reads the same commits that git log reads.
Common Mistakes
Getting stuck in the pager. A long log opens in a viewer. Press q to leave.
Expecting --author to match the whole name exactly. It matches any part, so --author="Navika" also matches "Navika Anamaneni".
Forgetting the -- before a file name. Without it, a file name that looks like a branch or a commit confuses Git. With --, Git knows that everything after it is a path.
Combining two --grep options and expecting both to match. Several --grep options match commits that contain any of them. Add --all-match to require all.
Using %ar in a script. "3 days ago" changes every day. Scripts should use fixed dates such as %ad with --date=iso.
Thinking git log shows other people's new work. It shows the commits in your local repository. To see teammates' new commits, fetch them first, as the lessons on remotes explain.
Under the Hood
git log doesn't read a list of commits, because there is none. It starts at HEAD, reads that commit, follows its parent line to the commit before, and keeps going until it reaches a commit with no parent. That is why a log is always in order from newest to oldest, and why it can be stopped after -n commits without reading the rest.
Filters are applied during that walk. For -- <path>, Git compares each commit's snapshot with its parent's and keeps the commit only if the file is different. This is also why the history of a renamed file stops at the rename unless you add --follow.
Interview Corner
How do you see a compact history of a repository?
git log --oneline, which shows one line per commit with the short ID and the subject. Add --graph --all to see every branch as a graph, and -n to limit the number of commits.
How do you find all commits that changed a particular file?
git log -- <path>. Add --oneline for a short list, -p to see the changes, and --follow to continue through renames.
How do you find commits by a particular author or with a particular word in the message?
git log --author="<name>" for the author and git log --grep="<word>" for the message. Filters of different kinds can be combined, for example with --since or a path.
What is the difference between git log --stat and git log -p?
--stat lists the files each commit changed, with the number of lines added and removed. -p shows the full patch: every added and removed line.
How do you count commits per author?
git shortlog -sn lists each author with their number of commits, sorted from most to fewest.
Practice
Challenge: who touched the fees? In the same repository, list the commits that changed fees.txt, one line each, with the short ID, the author's name and the subject. Then list the commits that did not come from Navika and contain the word "Add" in the subject.
What does Git print? Look at the commits from the start of this lesson and predict both lists before you open the solution.
Show solution
git log --format="%h %an: %s" -- fees.txt
git log --oneline --grep="Add" --author="Srihitha" --author="Advitha"bb5be3e Advitha Anamaneni: Add the hostel fee
7c6aa3e Srihitha Anamaneni: Add the late fee rule
03b7a0b Advitha Anamaneni: Fix the term 2 fee
fe87780 Navika Anamaneni: Add the term fees
bb5be3e (HEAD -> main) Add the hostel fee
7c6aa3e Add the late fee rule
32f03fa Add the fee calculatorfees.txt was changed by four commits. For the second list, two --author options match commits by either author, and --grep must also match, so "Fix the term 2 fee" is left out.
Checkpoint. In a project of your own, answer these with one git log command each: How many commits did you make last week? Which commit last changed your main source file? Which commits mention "fix" in the message?
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git log | Show the full history |
git log --oneline | One line per commit |
git log -5 | Only the five newest commits |
git log --stat | Show which files each commit changed |
git log -p -- <file> | Show every change to one file |
git log --oneline --graph --all | Show every branch as a graph |
git log --format="%h %an %s" | Print chosen fields |
git log --author="<name>" | Commits by one author |
git log --grep="<text>" | Commits whose message contains the text |
git log --since="2 weeks ago" | Commits from the last two weeks |
git shortlog -sn | Count commits per author |
Was this tutorial helpful?