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.
🎯 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
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:
git logOutput
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| Line | Meaning |
|---|---|
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 |
Author | Who made it (from your git config) |
Date | When it was made |
| Indented text | The 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:
git log --onelineOutput
1ce7ed1 (HEAD -> main) Add intro sentence ec605b9 Add stylesheet 6103896 Add home page
git show: inside one commit
git show followed by an id shows the commit's details and exactly what it changed:
git log --oneline -1
git show --stat HEADOutput
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:
printf '<p>Email: asha@example.com</p>\n' >> index.html
git diffOutput
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>
| Line | Meaning |
|---|---|
--- a/index.html and +++ b/index.html | The 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 space | Unchanged, 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:
printf '<p>Email: asha@example.com</p>\n' >> index.html
git add index.html
git diff
git diff --staged --statOutput
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.
| Command | Compares |
|---|---|
git diff | Working folder ↔ staging area (changes not staged yet) |
git diff --staged | Staging area ↔ last commit (what the next commit will contain) |
git diff abc1234 def5678 | Any 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.
- Run
git log --onelineand find the oldest commit (the bottom line). - Copy its short id and run
git show --statfollowed by that id.
Show the solution
git log --oneline
git show --stat --format="%h %s" HEAD~2Output
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
printf 'h1 { color: darkgreen; }\n' > styles.css
git diffOutput
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 loglists commits newest first;--onelinegives one line each.git show <id>shows one commit; HEAD means the commit you are on.git diffshows unstaged changes;git diff --stagedshows 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. Which command lists commits one per line?
2. You staged a change with git add. Which command shows it?
3. In a diff, what does a line starting with + mean?
4. What does HEAD~1 refer to?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.