Git: Beginner to Advanced
Inspecting changes
Review file changes and commits before acting.
Lesson 3 of 12
Reading: about 7 min
Lesson + practice: about 15 min
Difficulty: Beginner
On this page5 sections
Review file changes and commits before acting. Follow the animated example, then use the sections below to review each idea.
Visual walkthrough
Use the player controls or jump menu to inspect each step. The diagram changes as the example moves forward.
The player shows one focused example. The sections below explain the ideas behind each topic.
Before you stage, commit, merge or undo anything, look first. Git can show you exactly what changed in your files, what is about to be committed, what any past commit did, and how two branches differ. None of these commands change anything, so you can run them as often as you like.
This lesson follows notes.md from Chapter 1, as in the walkthrough above. Commit IDs in your repository will differ.
Working and staged diffs
A file can have three versions at once: the last commit, the staged copy, and the copy you are editing. The two diff commands compare different pairs:
git diffcompares your working files with the staged copy: edits you have not staged yet.git diff --stagedcompares the staged copy with the last commit: what your next commit will contain.
Read your first diff
Lines starting with - were removed and lines starting with + were added. The @@ -1 +1 @@ line says which lines are shown: line 1 before and line 1 after.
git diffdiff --git a/notes.md b/notes.md
index e965047..670a245 100644
--- a/notes.md
+++ b/notes.md
@@ -1 +1 @@
-Hello
+Hello, Git!| Part | Meaning |
|---|---|
| --- a/notes.md and +++ b/notes.md | The old and new version of the file |
| @@ -1 +1 @@ | Where the change is: starting line (and count) before and after |
| Line starting with - | Removed |
| Line starting with + | Added |
| Line starting with a space | Unchanged context around the change |
Stage it, and the plain diff goes quiet
After git add, the working file and the staged copy match, so git diff prints nothing. The change now appears under --staged instead.
git add notes.md
git diff
git diff --staged$ git add notes.md
$ git diff
$ git diff --staged
diff --git a/notes.md b/notes.md
index e965047..670a245 100644
--- a/notes.md
+++ b/notes.md
@@ -1 +1 @@
-Hello
+Hello, Git!Edit the file again after staging and the two commands show different things. MM in the short status means the file has a staged change and a newer unstaged one:
Two different diffs for one file
git diff shows only the extra ! you have not staged. git diff --staged still shows the version the next commit will save.
git status --short
git diff
git diff --staged$ git status --short
MM notes.md
$ git diff
diff --git a/notes.md b/notes.md
index 670a245..ce26356 100644
--- a/notes.md
+++ b/notes.md
@@ -1 +1 @@
-Hello, Git!
+Hello, Git!!
$ git diff --staged
diff --git a/notes.md b/notes.md
index e965047..670a245 100644
--- a/notes.md
+++ b/notes.md
@@ -1 +1 @@
-Hello
+Hello, Git!git show
git show displays one commit: its ID, author, date and message, followed by the changes it made compared with its parent. With no argument it shows HEAD, the commit you are on.
Look inside a commit
This is the commit that saved the staged version. The newer unstaged ! is not part of it.
git show HEADcommit 7d3f63c135e8436834adc3fad9231b59e5208b6c
Author: Alex Bennett <alex@example.com>
Date: Tue Oct 20 09:02:00 2026 +0100
Update greeting
diff --git a/notes.md b/notes.md
index e965047..670a245 100644
--- a/notes.md
+++ b/notes.md
@@ -1 +1 @@
-Hello
+Hello, Git!Just the summary
--stat replaces the full patch with a count of changed lines per file, which is quicker to scan for large commits.
git show --stat --oneline HEAD7d3f63c Update greeting
notes.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)You can show any commit by its ID, such as git show da840bf, or relative to HEAD: HEAD~1 is the commit before HEAD, HEAD~2 the one before that.
Readable git log views
The full git log is detailed but long. A few options make it far easier to find what you need:
One line per commit, and the shape of the history
--oneline gives a compact list. Adding --graph --all draws every branch, so you can see that search split off after Add emphasis.
git log --oneline
git log --oneline --graph --all$ git log --oneline
564b116 Add start note
684f680 Add emphasis
7d3f63c Update greeting
da840bf Add notes
$ git log --oneline --graph --all
* 564b116 Add start note
| * dc222ca Add search
|/
* 684f680 Add emphasis
* 7d3f63c Update greeting
* da840bf Add notesFilter and summarise
-2 limits the output to two commits and --stat adds files changed. -- search.js keeps only commits that touched that file; --all searches every branch, not just the one you are on.
git log --oneline --stat -2
git log --oneline --all -- search.js$ git log --oneline --stat -2
564b116 Add start note
notes.md | 1 +
1 file changed, 1 insertion(+)
684f680 Add emphasis
notes.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
$ git log --oneline --all -- search.js
dc222ca Add search| Option | Shows |
|---|---|
| --oneline | One line per commit: short ID and message |
| --graph --all | Every branch, drawn as a graph |
| --stat | Files changed in each commit |
| -p | The full patch for each commit |
| -n (for example -3) | Only the latest n commits |
| --author=name | Commits by one person |
| -- path | Commits that changed that file or folder |
Compare commits and branches
Give git diff two commits and it compares their saved snapshots. Your working files are not involved.
Compare two commits
HEAD~2 is Update greeting and HEAD is Add start note, so this shows everything that changed across the last two commits combined.
git diff HEAD~2 HEADdiff --git a/notes.md b/notes.md
index 670a245..80a78b3 100644
--- a/notes.md
+++ b/notes.md
@@ -1 +1,2 @@
-Hello, Git!
+Hello, Git!!
+Run npm start.Comparing branches has a trap. Two dots and three dots answer different questions:
Two branch tips versus “what this branch added”
main search compares the two tips directly, so it also reports notes.md, which only main changed. Three dots (main...search) compares search with the point where it split from main, so it shows only what the branch itself added. That is usually what you want before merging. main..search in git log lists the commits on search that main does not have.
git diff --stat main search
git diff --stat main...search
git log --oneline main..search$ git diff --stat main search
notes.md | 1 -
search.js | 1 +
2 files changed, 1 insertion(+), 1 deletion(-)
$ git diff --stat main...search
search.js | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline main..search
dc222ca Add searchYou staged a change, then edited the file again. Which command shows exactly what your next commit will contain?
Reveal answer
git diff --staged. It compares the staged copy with the last commit. Plain git diff would only show the newer edit you have not staged.
Before merging feature into main, you want to see only the changes made on feature. Which command?
Reveal answer
git diff main...feature, with three dots. It compares feature with the commit where it split from main, so changes made on main in the meantime do not appear.
Your progress
Reveal the check answer or reach the end to complete this lesson.