Git Foundations / Record Your Work

Inspect history

Lesson 7 of 33. Open to read. No account needed.

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

Five commits listed newest first, with illustrative short IDs and summaries. The newest, Add visa reminder, is HEAD and is labelled by the branch main. Below it are HEAD1, Remove umbrella from packing list; HEAD2, Add charger to packing list; HEAD3, Add travel dates; and HEAD4, Add packing list, the root commit. Each arrow points down to the parent.

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 0ac42a5 works as well as the full 40 characters.
  • HEAD, the commit you are currently on. Here, it is the newest commit on main.
  • HEAD~1, HEAD~2 and so on: the commit one, two or more steps back along the parent links. In the diagram, HEAD~1 is the parent of HEAD, and HEAD~4 is 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 -a and -m together: 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.
  1. 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"
    
  2. Read the full history:

    git log
    

    Five commits appear, newest first. Notice that one of them, "Add travel dates", has a different author. Press q if the page view opens.

  3. Count the commits:

    git log --oneline
    

    Illustrative 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
    
  4. Question: which commits changed itinerary.md, and who made them?

    git log --oneline -- itinerary.md
    git log -- itinerary.md
    

    Only "Add travel dates" changed the file, and the full entry shows its author: Ravi Kumar.

  5. Question: when was "Umbrella" removed?

    git log -S Umbrella --oneline
    

    Two commits appear: "Add packing list", which added the text, and "Remove umbrella from packing list", which removed it. -S finds both, because both changed how often the text appears. Show the newer one, using its ID from your own output, to confirm:

    git show 0ac42a5
    

    In the changes, the line -- Umbrella is the removed line. The first - marks a removal; the second is the list dash in the file.

  6. Draw the shape of the history:

    git log --graph --oneline
    

    With 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 seeLikely causeNext step
A : or (END) at the bottom of the screenGit is showing output one page at a timePress Space to scroll or q to quit
git log -S umbrella finds nothingThe search is case-sensitive; the file says UmbrellaMatch the exact capitalisation
unknown revision or path not in the working treeA mistyped ID, or a name that goes past the root commitCopy the ID from git log --oneline; count the commits
Fewer than five commitsA setup command failed partwayRemove the folder and run the setup block again
  1. 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:

  1. Which commit added "Phone charger", and who made it?
  2. Which file did the newest commit change, and how many lines did it add?
  3. What is HEAD~4? What happens if you ask for git 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.

  1. Which commits are HEAD~1 and HEAD~3?
  2. You make a new commit, F. Which commit is HEAD~1 now? Does D's commit ID still name D?

Answer and Reasoning

Independent task

  1. git log -S "Phone charger" --oneline finds "Add charger to packing list". Running git show on that ID, or git log for 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.
  2. git show --stat shows the newest commit changed checklist.md with 4 insertions: the blank line, the ## Documents heading, another blank line and the visa reminder.
  3. git show HEAD~4 shows "Add packing list", the root commit. git show HEAD~5 fails with unknown revision or path not in the working tree, because the root commit has no parent: there is no fifth step back.

Understanding check

  1. HEAD~1 is D, one step back from E. HEAD~3 is B, three steps back.
  2. After F, HEAD is F, so HEAD~1 is E. The relative names shift with HEAD. 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.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy