CH 11 / 75Part 2: Git Basics — Working Locally

Reading History with git log

What you will learn on this page

  • What each line of git log output represents
  • How to switch the view between a list, changed files and diffs, depending on the detail you need
  • How to filter history by file, message and time period
  • How to format the output so it's easy for you to read
On this page15 items

In Part 2, #10, you read changes that haven’t been recorded yet. This page does the opposite: it reads changes you’ve already recorded. The more commits you make, the more your history becomes a reference you can consult for “when, who, and why something changed.”

Setup: record the changes from Part 2, #10#

Record the changes you made in Part 2, #10 as two separate commits. draft.txt was just for practice, so delete it.

text5 lines
cd ~/git-practice
rm draft.txt
git commit -m "Change milk to soy milk"
git add todo.txt
git commit -m "Add bank to to-do list"

The first git commit records only memo.txt, which you staged in Part 2, #10.

The basic output of git log#

text14 lines
git log -n 2

# Output:
# commit f27f4bb92692b4e9580ed935ab525c283333c1c9
# Author: Taro Yamada <[email protected]>
# Date:   Thu Oct 22 00:32:34 2026 +0900
#
#     Add bank to to-do list
#
# commit 9ec120469b007f31fbc0c60be476e7c26ed94ef7
# Author: Taro Yamada <[email protected]>
# Date:   Thu Oct 22 00:32:34 2026 +0900
#
#     Change milk to soy milk

-n 2 means “only the two newest.” Without it, every commit is shown, newest first (if it’s long, press q to quit).

LineContents
commit f27f4bb…The commit hash (40 characters). The ID that points to this record
Author:Who recorded it. The user.name and user.email you set in Part 2, #06
Date:When it was recorded. The trailing +0900 indicates Japan time
Indented linesThe commit message

You can specify a hash with just its first seven or so characters. When pointing to a commit in other commands, a short form like f27f4bb is enough.

Switching the level of detail#

Change the amount of information to match what you want to know.

List one per line: –oneline#

text8 lines
git log --oneline

# Output:
# f27f4bb Add bank to to-do list
# 9ec1204 Change milk to soy milk
# 1fe223f Create to-do list
# c9990eb Add eggs to shopping memo
# d9110b5 Create shopping memo

Only the first line of each commit message is shown. Part 2, #09 said to “write the first line so it alone tells you what the commit is” precisely so you can read it in this view.

See which files changed: –stat#

text11 lines
git log --stat -n 1

# Output:
# commit f27f4bb92692b4e9580ed935ab525c283333c1c9
# Author: Taro Yamada <[email protected]>
# Date:   Thu Oct 22 00:32:34 2026 +0900
#
#     Add bank to to-do list
#
#  todo.txt | 1 +
#  1 file changed, 1 insertion(+)

See the diff too: -p#

text11 lines
git log -p -n 1

# Output:
# commit f27f4bb92692b4e9580ed935ab525c283333c1c9
# (snip)
#     Add bank to to-do list
#
# diff --git a/todo.txt b/todo.txt
# (snip)
#  Book a dentist appointment
# +Go to the bank

You read the diff the same way as in Part 2, #10. -p produces long output, so the usual approach is to combine it with -n to limit the count.

Filtering#

Filter by file#

text6 lines
git log --oneline -- memo.txt

# Output:
# 9ec1204 Change milk to soy milk
# c9990eb Add eggs to shopping memo
# d9110b5 Create shopping memo

Put a file name after -- and only the commits that changed that file are shown. -- is a separator meaning “file names from here on,” which prevents confusion when a file name could be mistaken for another option.

Filter by message#

text5 lines
git log --oneline --grep="memo"

# Output:
# c9990eb Add eggs to shopping memo
# d9110b5 Create shopping memo

--grep searches within commit messages. The more specific your messages, the easier they are to find here.

Filter by time period or person#

text2 lines
git log --oneline --since="1 week ago"
git log --oneline --author="Taro"

--since means “from this date/time on,” and --until means “up to this date/time.” You can use dates like "2026-10-01" as well as expressions like "2 days ago". --author shows commits whose name or email address partially matches.

Formatting the output#

text5 lines
git log --format="%h %an %ad %s" --date=short -n 2

# Output:
# f27f4bb Taro Yamada 2026-10-22 Add bank to to-do list
# 9ec1204 Taro Yamada 2026-10-22 Change milk to soy milk

--format lets you decide which fields to show and in what order. %h is the short hash, %an the author’s name, %ad the date, and %s the first line of the message. --date=short puts the date in YYYY-MM-DD form.

Quick reference#

What you want to knowCommand
Skim the historygit log --oneline
See just the last fewgit log -n 5
See which files each commit changedgit log --stat
See each commit’s diffgit log -p -n 1
See the history of one filegit log --oneline -- filename
Search by messagegit log --oneline --grep="phrase"
Filter by time periodgit log --since="2026-10-01" --until="2026-10-31"
Filter by who recorded itgit log --author="name"
See the shape of branches toogit log --oneline --graph
Change the output formatgit log --format="%h %ad %s" --date=short

--graph draws the shape of branches splitting and joining with symbols. Right now there’s only one line of history, so you’ll just see a column of *, but it becomes meaningful from Part 4, #22.

There’s also a way to search by the contents of the diff, such as “when was a certain phrase added or removed?” This is covered in Part 5, #31.

FAQ#

Q. File names with Japanese characters are shown like "\350\262\267…".
A. By default, Git displays file names containing non-ASCII characters as escape codes. The following setting makes them display as they are.

text1 lines
git config --global core.quotepath false

Q. Can I open the contents of an old commit and view it as a file?
A. git show hash:filename displays the contents of the file as of that commit (for example, git show d9110b5:memo.txt). The file in your working tree doesn’t change.

Summary#

  • git log shows each commit’s hash, author, date and message, newest first
  • You can specify a hash with its first seven or so characters
  • Switch the level of detail with --oneline, --stat and -p
  • Filter with -- filename, --grep, --since and --author
  • Format the output freely with --format

Next page#

12. Excluding Files with .gitignore — See how to keep files containing passwords and automatically generated junk files out of your records from the start.