TExcelTAD
Home › Git › Git History

Git History

The history is the reason to use Git in the first place. git log is the main command for exploring it, and it has options for almost every question you might ask: what changed, when, by whom, in which files.

All examples on this page first build the same small repository with this setup script, then ask different questions about it:

bash
mkdir todo && cd todo && git init -q
echo "- buy milk" > list.txt; git add .; git commit -q -m "Start todo list"
echo "- call Bob" >> list.txt; git commit -q -am "Add call reminder"
echo "# Todo app" > README.md; git add .; git commit -q -m "Add README"
git -c user.name="Grace Hopper" -c user.email=grace@example.com commit -q --allow-empty -m "Review list"
echo "- buy milk (2 l)" > list.txt; echo "- call Bob" >> list.txt; git commit -q -am "Fix milk amount"

(The -c options make Grace Hopper the author of one commit, and --allow-empty makes a commit with no file changes.)

The full log

bash · try it yourself
mkdir todo && cd todo && git init -q
echo "- buy milk" > list.txt; git add .; git commit -q -m "Start todo list"
echo "- call Bob" >> list.txt; git commit -q -am "Add call reminder"
echo "# Todo app" > README.md; git add .; git commit -q -m "Add README"
git -c user.name="Grace Hopper" -c user.email=grace@example.com commit -q --allow-empty -m "Review list"
echo "- buy milk (2 l)" > list.txt; echo "- call Bob" >> list.txt; git commit -q -am "Fix milk amount"
git log -2
echo "=== one line per commit:"
git log --oneline
commit 94f63f370c6072adaf0dfbcf6ea093794173ae37
Author: Ada Lovelace <ada@example.com>
Date:   Thu Jan 15 10:00:00 2026 +0000

    Fix milk amount

commit b03530b07cd6fb286ba32affa260881af41af5ea
Author: Grace Hopper <grace@example.com>
Date:   Thu Jan 15 10:00:00 2026 +0000

    Review list
=== one line per commit:
94f63f3 Fix milk amount
b03530b Review list
f4d027f Add README
0101e50 Add call reminder
82dfe6e Start todo list

The newest commit comes first. In a real terminal Git opens long output in a pager (press q to quit, space for the next page).

Filtering the log

Option Shows
-n 3 or -3 the last three commits
--author="Grace" commits whose author matches
--grep="milk" commits whose message matches
--since="2 weeks ago", --until=2026-01-01 a date range
-- list.txt only commits that touched this path
-S"call Bob" commits that added or removed that text ("pickaxe")
bash · try it yourself
mkdir todo && cd todo && git init -q
echo "- buy milk" > list.txt; git add .; git commit -q -m "Start todo list"
echo "- call Bob" >> list.txt; git commit -q -am "Add call reminder"
echo "# Todo app" > README.md; git add .; git commit -q -m "Add README"
git -c user.name="Grace Hopper" -c user.email=grace@example.com commit -q --allow-empty -m "Review list"
echo "- buy milk (2 l)" > list.txt; echo "- call Bob" >> list.txt; git commit -q -am "Fix milk amount"
echo "== by Grace:";        git log --oneline --author=Grace
echo "== mentioning milk:"; git log --oneline -i --grep=MILK
echo "== touching README:"; git log --oneline -- README.md
echo "== added 'call Bob':"; git log --oneline -S"call Bob"
== by Grace:
b03530b Review list
== mentioning milk:
94f63f3 Fix milk amount
== touching README:
f4d027f Add README
== added 'call Bob':
0101e50 Add call reminder

Seeing the changes

-p (patch) adds the diff of each commit, and --stat a summary of changed files:

bash · try it yourself
mkdir todo && cd todo && git init -q
echo "- buy milk" > list.txt; git add .; git commit -q -m "Start todo list"
echo "- call Bob" >> list.txt; git commit -q -am "Add call reminder"
echo "# Todo app" > README.md; git add .; git commit -q -m "Add README"
git -c user.name="Grace Hopper" -c user.email=grace@example.com commit -q --allow-empty -m "Review list"
echo "- buy milk (2 l)" > list.txt; echo "- call Bob" >> list.txt; git commit -q -am "Fix milk amount"
git log -1 -p --format="%h %s"
echo "=== stat:"
git log --stat --format="%h %s" -3
94f63f3 Fix milk amount

diff --git a/list.txt b/list.txt
index db33186..e9ef224 100644
--- a/list.txt
+++ b/list.txt
@@ -1,2 +1,2 @@
-- buy milk
+- buy milk (2 l)
 - call Bob
=== stat:
94f63f3 Fix milk amount

 list.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
b03530b Review list
f4d027f Add README

 README.md | 1 +
 1 file changed, 1 insertion(+)

Custom formats

--format takes placeholders, which is handy for reports and scripts:

Placeholder Meaning
%H / %h full / short hash
%an, %ae author name, email
%ad, %as author date, short date (YYYY-MM-DD)
%s subject (first line of the message)
%d decorations (branch and tag names)
bash · try it yourself
mkdir todo && cd todo && git init -q
echo "- buy milk" > list.txt; git add .; git commit -q -m "Start todo list"
echo "- call Bob" >> list.txt; git commit -q -am "Add call reminder"
echo "# Todo app" > README.md; git add .; git commit -q -m "Add README"
git -c user.name="Grace Hopper" -c user.email=grace@example.com commit -q --allow-empty -m "Review list"
echo "- buy milk (2 l)" > list.txt; echo "- call Bob" >> list.txt; git commit -q -am "Fix milk amount"
git log --format="%h %as %<(13)%an %s" --reverse
echo "=== commits per author:"
git shortlog -s -n HEAD
82dfe6e 2026-01-15 Ada Lovelace  Start todo list
0101e50 2026-01-15 Ada Lovelace  Add call reminder
f4d027f 2026-01-15 Ada Lovelace  Add README
b03530b 2026-01-15 Grace Hopper  Review list
94f63f3 2026-01-15 Ada Lovelace  Fix milk amount
=== commits per author:
     4	Ada Lovelace
     1	Grace Hopper

--reverse lists the oldest first, %<(13) pads the next field to 13 characters, and git shortlog -s -n HEAD counts commits per author. (Give shortlog a revision such as HEAD in scripts: without one, and without a terminal, it reads a log from standard input instead.)

Referring to older commits

HEAD~1 is the parent of HEAD, HEAD~2 its grandparent, and so on. git show <commit>:<path> prints a file as it was then:

bash · try it yourself
mkdir todo && cd todo && git init -q
echo "- buy milk" > list.txt; git add .; git commit -q -m "Start todo list"
echo "- call Bob" >> list.txt; git commit -q -am "Add call reminder"
git show HEAD~1:list.txt
echo "---"
git log --oneline HEAD~1..HEAD
- buy milk
---
0101e50 Add call reminder

A..B means "commits reachable from B but not from A", here just the latest commit.

Test yourself

Which command shows the last 3 commits with one line each?

  1. git log -3 --oneline
  2. git show 3
  3. git history 3
  4. git status -3

How do you find the commits that added or removed the text API_URL?

  1. git log --grep=API_URL
  2. git log -S"API_URL"
  3. git log --author=API_URL
  4. git find API_URL

What does HEAD~2 refer to?

  1. The second branch
  2. The grandparent of the current commit
  3. Two files back
  4. The second tag