Nishaglobal Education logo

Viewing history and changes

Explore a project's past with git log and git show, and see exactly what changed with git diff and git diff --staged, before and after staging, line by line.

Lesson 3 of 1130 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Read the history with git log and git log --oneline
  • Look inside one commit with git show
  • See unstaged and staged changes with git diff
Outline
  1. What you'll learn and why it matters
  2. git log: the history
  3. git show: inside one commit
  4. git diff: what have I changed?
  5. Practice
  6. Common mistakes
  7. Recap
  8. Quiz
  9. Mark lesson complete

What you'll learn and why it matters

History is only useful if you can read it. When something breaks, the first questions are *what changed?* and *when?*. Git can answer both in seconds. You will also use git diff before every commit to check exactly what you are about to save.

git log: the history

Asha's project now has three commits. git log lists them, newest first:

Terminal
git log

Output

commit 1ce7ed113ed5523f2775c5ca1df3736e709453ab (HEAD -> main)
Author: Asha Kumar <asha@example.com>
Date:   Fri Nov 20 10:00:00 2026 +0530

    Add intro sentence

commit ec605b9928155f08bf02c45077e06608d28a4de6
Author: Asha Kumar <asha@example.com>
Date:   Thu Nov 19 14:40:00 2026 +0530

    Add stylesheet

commit 6103896250e8973ddf2a424f5b1ae1e46507d37c
Author: Asha Kumar <asha@example.com>
Date:   Wed Nov 18 09:15:00 2026 +0530

    Add home page
LineMeaning
commit 1ce7ed1…The commit's full id: 40 characters, unique to this commit
(HEAD -> main)Where you are now: HEAD points at the latest commit on the main branch
AuthorWho made it (from your git config)
DateWhen it was made
Indented textThe commit message

Long logs open in a scrolling view: press Space for the next page and q to quit. For a compact list, one commit per line:

Terminal
git log --oneline

Output

1ce7ed1 (HEAD -> main) Add intro sentence
ec605b9 Add stylesheet
6103896 Add home page
6103896Add home pageec605b9Add stylesheet1ce7ed1Add intro sentencemainHEADEach commit points back to its parent.
Three commits, oldest at the top: Add home page, Add stylesheet, Add intro sentence. Each commit points back to the one before it. The main label and HEAD point at the newest commit.

git show: inside one commit

git show followed by an id shows the commit's details and exactly what it changed:

Terminal
git log --oneline -1
git show --stat HEAD

Output

1ce7ed1 (HEAD -> main) Add intro sentence
commit 1ce7ed113ed5523f2775c5ca1df3736e709453ab (HEAD -> main)
Author: Asha Kumar <asha@example.com>
Date:   Fri Nov 20 10:00:00 2026 +0530

    Add intro sentence

 index.html | 1 +
 1 file changed, 1 insertion(+)

HEAD means *the commit I am on now*. --stat lists the files changed instead of every line. Without it, you see the full changes.

git diff: what have I changed?

Asha adds a contact line to her page but has not staged it yet. git diff shows the difference between the working folder and the last commit:

Terminal
printf '<p>Email: asha@example.com</p>\n' >> index.html
git diff

Output

diff --git a/index.html b/index.html
index c99ddce..02a5ed6 100644
--- a/index.html
+++ b/index.html
@@ -1,3 +1,4 @@
 <h1>Asha Kumar</h1>
 <p>Junior web developer</p>
 <p>I build accessible websites.</p>
+<p>Email: asha@example.com</p>
LineMeaning
--- a/index.html and +++ b/index.htmlThe old version (a) and the new version (b)
@@ -1,3 +1,4 @@Where the change is: lines 1 to 3 before, lines 1 to 4 after
Line starting with a spaceUnchanged, shown for context
Line starting with +Added
Line starting with -Removed

Once a change is staged, plain git diff shows nothing for it. To see what you are about to commit, use --staged:

Terminal
printf '<p>Email: asha@example.com</p>\n' >> index.html
git add index.html
git diff
git diff --staged --stat

Output

 index.html | 1 +
 1 file changed, 1 insertion(+)

Plain git diff printed nothing, because nothing is left unstaged. git diff --staged compares the staging area with the last commit.

CommandCompares
git diffWorking folder ↔ staging area (changes not staged yet)
git diff --stagedStaging area ↔ last commit (what the next commit will contain)
git diff abc1234 def5678Any two commits

Practice

Guided exercise

Explore the history

In Asha's three-commit project, show the history in one-line form, then show which files the oldest commit changed.

  1. Run git log --oneline and find the oldest commit (the bottom line).
  2. Copy its short id and run git show --stat followed by that id.
Show the solution
Solution
git log --oneline
git show --stat --format="%h %s" HEAD~2

Output

1ce7ed1 (HEAD -> main) Add intro sentence
ec605b9 Add stylesheet
6103896 Add home page
6103896 Add home page

 index.html | 2 ++
 1 file changed, 2 insertions(+)

How to get there: HEAD~2 means *two commits before HEAD*, which is the oldest one here; you could also paste its id. --format="%h %s" prints just the short id and message, so the output is compact. The oldest commit added only index.html.

Your turn

Check before committing

Change the stylesheet so headings are dark green, then show the unstaged change with git diff.

Show the solution
Solution
printf 'h1 { color: darkgreen; }\n' > styles.css
git diff

Output

diff --git a/styles.css b/styles.css
index bb5b0d6..2994841 100644
--- a/styles.css
+++ b/styles.css
@@ -1 +1 @@
-h1 { color: navy; }
+h1 { color: darkgreen; }

How to get there: A changed line shows as one line removed (-) and one line added (+). Nothing is staged yet, so plain git diff is the right command.

Common mistakes

Getting stuck in a long log

You see: A screen with : or (END) at the bottom

Fix: Press q to quit.

Expecting git diff to show staged changes

You see: It prints nothing even though you changed files

Fix: Use git diff --staged after git add.

Typing a wrong or too-short commit id

You see: fatal: ambiguous argument

Fix: Copy at least 7 characters from git log --oneline.

Recap

  • git log lists commits newest first; --oneline gives one line each.
  • git show <id> shows one commit; HEAD means the commit you are on.
  • git diff shows unstaged changes; git diff --staged shows what will be committed.
  • Read diffs as + added and - removed lines, with unchanged lines for context.

Key terms

Commit id (hash)
A unique 40-character code for a commit; the first 7 characters are usually enough.
HEAD
A pointer to the commit you are currently on.
Diff
A line-by-line comparison showing what was added and removed.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. Which command lists commits one per line?

  2. 2. You staged a change with git add. Which command shows it?

  3. 3. In a diff, what does a line starting with + mean?

  4. 4. What does HEAD~1 refer to?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.