What History Can Tell You
A repository's history answers questions that would otherwise depend on
someone's memory: when a line was added, who changed a file, what a particular
change contained and why it was made. This lesson uses two commands. git log
lists commits, and git show displays one commit in detail. Together they
answer most questions about a project's past.
Reading git log
Run in a repository, git log lists commits from newest to oldest:
commit d0d7d8536b4a1af776f599bb38616427a6d8cec6
Author: Asha Rao <asha@example.com>
Date: Fri Oct 9 00:30:22 2026 +0530
Add visa reminder
commit 0ac42a5c911b7fe5f86c049fefd7e28528e4cae1
Author: Asha Rao <asha@example.com>
Date: Fri Oct 9 00:30:22 2026 +0530
Remove umbrella from packing list
This is illustrative output. Each entry gives the full commit ID, the author, the date (with the time zone offset from UTC) and the message.
When the output is longer than your terminal window, Git shows it one page at a
time. Press Space for the next page and q to return to the prompt. A
colon or (END) at the bottom of the screen means you are in this page view.
These options change what git log shows. You can combine them, as in
git log --oneline -3.
git log --oneline # one line per commit: short ID and summary
git log -3 # only the newest three commits (same as -n 3)
git log --graph # a drawing of how commits connect, on the left
git log -p # each commit's changes
git log --stat # which files each commit changed, and how many lines
git log -- <file> # only commits that changed that file
git log -S "<text>" # only commits that changed how often that text appears
Two of these need more explanation.
-- <file> limits the list to commits that touched the file. The --
separates file names from everything else on the command line, so Git never
mistakes a file name for something else.
-S "<text>" searches the changes in each commit, not the messages. It
finds commits that added or removed the text, so it is the quickest way to
answer "when did this line appear?" or "when did it disappear?". The search is
case-sensitive: -S Umbrella and -S umbrella find different things.
Looking at One Commit
git show displays one commit: its ID, author, date and message, followed by
its changes. On its own it shows the newest commit; give it an ID or another
name to show a different one:
git show
git show 0ac42a5
git show --stat 0ac42a5
--stat replaces the changes with a summary of the files changed and the
number of lines added and removed.
Naming Commits

Commands that take a commit accept several kinds of name:
- A commit ID, full or shortened. Git accepts any beginning of the ID that
matches only one commit, so
0ac42a5works as well as the full 40 characters. HEAD, the commit you are currently on. Here, it is the newest commit onmain.HEAD~1,HEAD~2and so on: the commit one, two or more steps back along the parent links. In the diagram,HEAD~1is the parent ofHEAD, andHEAD~4is the root commit, the first one, which has no parent.
These names are relative to where HEAD is now. After you make a new commit,
HEAD moves to it, and HEAD~1 then means the commit that was HEAD a moment
ago. A commit ID, by contrast, always names the same commit.
Exercise: Answer Questions About a History
The setup builds a small history about planning a trip. You will then answer
questions using only git log and git show, checking each answer against the
output that proves it.
Starting state: Git configured as in the configuration lesson.
The setup uses three things you have not run before:
cat >> file <<'EOF'appends: it adds the text to the end of the file instead of replacing it, which a single>would do.git commit -am "<message>"is-aand-mtogether: it stages every modified tracked file, as explained in the previous lesson, then commits with that message. Each setup commit here changes one file, so nothing unwanted slips in.--author="Ravi Kumar <ravi@example.com>"records one commit as if a teammate had made it, so the history has two authors.
-
Run the setup. Copy the whole block:
mkdir -p ~/git-practice/history-questions cd ~/git-practice/history-questions git init -b main cat > checklist.md <<'EOF' # Packing list - Toothbrush - Umbrella - Water bottle EOF git add checklist.md git commit -m "Add packing list" cat > itinerary.md <<'EOF' # Itinerary Leave on Friday, return on Monday. EOF git add itinerary.md git commit -m "Add travel dates" --author="Ravi Kumar <ravi@example.com>" cat >> checklist.md <<'EOF' - Phone charger EOF git commit -am "Add charger to packing list" cat > checklist.md <<'EOF' # Packing list - Toothbrush - Water bottle - Phone charger EOF git commit -am "Remove umbrella from packing list" cat >> checklist.md <<'EOF' ## Documents - Check visa requirements EOF git commit -am "Add visa reminder" -
Read the full history:
git logFive commits appear, newest first. Notice that one of them, "Add travel dates", has a different author. Press q if the page view opens.
-
Count the commits:
git log --onelineIllustrative output:
d0d7d85 Add visa reminder 0ac42a5 Remove umbrella from packing list 35c642a Add charger to packing list 73b81d4 Add travel dates d6a92af Add packing list -
Question: which commits changed
itinerary.md, and who made them?git log --oneline -- itinerary.md git log -- itinerary.mdOnly "Add travel dates" changed the file, and the full entry shows its author: Ravi Kumar.
-
Question: when was "Umbrella" removed?
git log -S Umbrella --onelineTwo commits appear: "Add packing list", which added the text, and "Remove umbrella from packing list", which removed it.
-Sfinds both, because both changed how often the text appears. Show the newer one, using its ID from your own output, to confirm:git show 0ac42a5In the changes, the line
-- Umbrellais the removed line. The first-marks a removal; the second is the list dash in the file. -
Draw the shape of the history:
git log --graph --onelineWith no branches, the graph is a single column of
*marks, one per commit. It becomes more useful in the branching module.
Success: you identified five commits; "Add travel dates" by Ravi Kumar as
the only commit touching itinerary.md; and the commits that added and removed
"Umbrella", each confirmed with git show.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
A : or (END) at the bottom of the screen | Git is showing output one page at a time | Press Space to scroll or q to quit |
git log -S umbrella finds nothing | The search is case-sensitive; the file says Umbrella | Match the exact capitalisation |
unknown revision or path not in the working tree | A mistyped ID, or a name that goes past the root commit | Copy the ID from git log --oneline; count the commits |
| Fewer than five commits | A setup command failed partway | Remove the folder and run the setup block again |
-
Clean up:
cd ~/git-practice ls rm -rf history-questions ls
Independent task: before cleaning up, answer these using git log and
git show, and note the command that proves each answer:
- Which commit added "Phone charger", and who made it?
- Which file did the newest commit change, and how many lines did it add?
- What is
HEAD~4? What happens if you ask forgit show HEAD~5, and why?
Check Your Understanding
In a history of commits A, B, C, D and E, made in that order, HEAD is E.
- Which commits are
HEAD~1andHEAD~3? - You make a new commit, F. Which commit is
HEAD~1now? Does D's commit ID still name D?
Answer and Reasoning
Independent task
git log -S "Phone charger" --onelinefinds "Add charger to packing list". Runninggit showon that ID, orgit logfor it, shows the author: your own name, because the setup made that commit with your configuration. The quotes are needed because the text contains a space.git show --statshows the newest commit changedchecklist.mdwith 4 insertions: the blank line, the## Documentsheading, another blank line and the visa reminder.git show HEAD~4shows "Add packing list", the root commit.git show HEAD~5fails withunknown revision or path not in the working tree, because the root commit has no parent: there is no fifth step back.
Understanding check
HEAD~1is D, one step back from E.HEAD~3is B, three steps back.- After F,
HEADis F, soHEAD~1is E. The relative names shift withHEAD. D's ID still names D: an ID always names the same commit, which is why IDs are the safest way to refer to one particular commit.