Git Log: Viewing History

Updated · 14 min read
easyGit 2.53gitgit-loghistoryshortloginterview

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:

Terminal
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"
ls
Output
FeeCalculator.java
README.md
fees.txt

ls 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:

Terminal
git log -2
Output
commit 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 app

Each entry has four parts:

PartMeaning
commit and a long IDThe 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 textThe 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:

Terminal
git log --oneline
Output
bb5be3e (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 README

This 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

Terminal
git log [options] [<commit>] [-- <path>...]
OptionIt is used to
--onelineShow one line per commit: short ID and subject
-<n> or -n <n>Show only the newest n commits
--statList the files each commit changed, with counts
-pShow the full changes (the patch) of each commit
--graphDraw the history as a graph, useful once branches appear
--allInclude every branch, not only the current one
--reverseShow the oldest commit first
--format="..."Print the fields you choose
The parts of git log --oneline

Which Files Changed: --stat

--stat adds a short list of the files each commit touched:

Terminal
git log --stat -2
Output
commit 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:

Terminal
git log -p -1
Output
commit 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,000

The 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:

Terminal
git log --format="%h  %ad  %an  %s" --date=format:"%Y-%m-%d %H:%M"
Output
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
CodePrints
%H / %hFull / short commit ID
%an / %aeAuthor name / email
%adAuthor date, in the style chosen with --date
%arAuthor date, relative, such as "3 days ago"
%cn / %cdCommitter name / date
%sSubject
%bBody
%dBranch and tag names, such as (HEAD -> main)
%nA 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:

Terminal
git log --oneline --author="Advitha"
Output
bb5be3e (HEAD -> main) Add the hostel fee
03b7a0b Fix the term 2 fee

By message. --grep matches text in the commit message. -i ignores upper and lower case:

Terminal
git log --oneline -i --grep="fee"
Output
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 fees

By file. Put the path after -- to see only the commits that changed that file:

Terminal
git log --oneline -- FeeCalculator.java
Output
7c6aa3e Add the late fee rule
32f03fa Add the fee calculator

By 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:

Terminal
git log --oneline --since="2026-01-05 10:12" --until="2026-01-05 10:17"
Output
7c6aa3e Add the late fee rule
03b7a0b Fix the term 2 fee
32f03fa Add the fee calculator

In 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:

Terminal
git log --oneline --author="Advitha" -- fees.txt
Output
bb5be3e (HEAD -> main) Add the hostel fee
03b7a0b Fix the term 2 fee
FilterKeeps 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:

Terminal
git shortlog -sn HEAD
git rev-list --count HEAD
Output
     3	Navika Anamaneni
     2	Advitha Anamaneni
     2	Srihitha Anamaneni
7

git 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
Terminal
git log --format="%h %an: %s" -- fees.txt
git log --oneline --grep="Add" --author="Srihitha" --author="Advitha"
Output
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 calculator

fees.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?

Quick check: Git log

4 questions
1Which command shows one line per commit?
2How do you list only the commits that changed fees.txt?
3Which option shows the files each commit changed, with counts of added and removed lines?
4What does git shortlog -sn show?

Key Takeaways

Quick reference

CommandIt is used to
git logShow the full history
git log --onelineOne line per commit
git log -5Only the five newest commits
git log --statShow which files each commit changed
git log -p -- <file>Show every change to one file
git log --oneline --graph --allShow 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 -snCount commits per author

Was this tutorial helpful?

Lesson 17 of 106