Skip to content
$ lynuxpro
← Git

LESSON 4 OF 21 · BEGINNER · 18 MIN

Viewing history and changes

HOW IT WORKSWhat each diff compares

git diffWorking treeyour editsStaging areanext commitHEADlast commitHEAD~3older commit
1/4

git diff with no arguments shows what you changed but have not staged yet.

History is Git's superpower. When something breaks, the first questions are always "what changed, when, and who did it?" This lesson teaches the commands that answer them: git log to browse history, git show to inspect one commit, and git diff to compare any two states of your project.

The examples use a real-looking project with history. Clone it in the practice terminal:

git clone https://gitlab.com/acme/shop.gitcd shop
SHELLbashutf-8 · 2L

In simple words

  • git log: shows the list of commits, newest first.
  • HEAD: a pointer to the commit you are on right now. HEAD~1 means one commit before it.
  • Diff: the exact lines that changed between two versions: lines removed start with -, lines added start with +.
  • git show: shows one commit's details and its changes.

git log: the history

git log -n 2
SHELLbashutf-8 · 1L
commit ce797e57d95fda935bce006ffcabeab2f0e5a9ff (HEAD -> main, origin/main)Author: Leela Kumar <leela@acme.dev>Date:   Fri Nov 6 22:00:00 2026 +0000    Add CI pipelinecommit d2c0bba05f98d949a6aa150bcef4a50607278654Author: Priya Sharma <priya@acme.dev>Date:   Fri Nov 6 10:00:00 2026 +0000    Report version in /health
OUTPUTterminalutf-8 · 11L

Each commit has a 40-character hash that identifies it uniquely, an author, a date and a message. The labels in brackets are references pointing at that commit: HEAD (where you are), main (your branch) and origin/main (the server's branch, as last seen).

The one-line view is what you will use most:

git log --oneline
SHELLbashutf-8 · 1L
ce797e5 (HEAD -> main, origin/main) Add CI pipelined2c0bba Report version in /healthc82322f Bump dependencies and tidy pricinga7b39ba Add Dockerfile2ff258c Add /total endpointe8b5455 (tag: v1.0.0) Document how to run the appdd73235 Add pricing with 18% GSTdd73235 Add Flask app with health endpointd512790 Initial commit
OUTPUTterminalutf-8 · 9L

The first seven characters of a hash are almost always enough to identify a commit, so you can type git show c82322f.

Filtering history

OptionShows
-n 5 or -5only the last 5 commits
--author=Ravicommits by an author (partial match)
--grep=taxcommits whose message contains a word
--since="2 weeks ago"commits in a time range (--until too)
-- path/filecommits that changed a file or folder
--statwhich files changed and by how much
-pthe full diff of each commit
--graph --allbranches and merges as a drawing, all branches
git log --oneline --author=Ravigit log --oneline -- pricing.pygit log --stat -n 1
SHELLbashutf-8 · 3L
c82322f Bump dependencies and tidy pricing2ff258c Add /total endpointdd73235 Add pricing with 18% GSTc82322f Bump dependencies and tidy pricingdd73235 Add pricing with 18% GSTcommit ce797e57d95fda935bce006ffcabeab2f0e5a9ff (HEAD -> main, origin/main)Author: Leela Kumar <leela@acme.dev>Date:   Fri Nov 6 22:00:00 2026 +0000    Add CI pipeline .gitlab-ci.yml |   7 +++++++ 1 file changed, 7 insertions(+)
OUTPUTterminalutf-8 · 13L

git log -- pricing.py is one of the most useful commands in an incident: it lists only the commits that touched the file you suspect.

Custom formats

--format builds your own one-line view. Common placeholders: %h short hash, %an author name, %ar relative date, %ad date, %s subject.

git log --format="%h %an %ar %s" -n 3
SHELLbashutf-8 · 1L
ce797e5 Leela Kumar 3 weeks ago Add CI pipelined2c0bba Priya Sharma 3 weeks ago Report version in /healthc82322f Ravi Kumar 3 weeks ago Bump dependencies and tidy pricing
OUTPUTterminalutf-8 · 3L

Naming commits: HEAD, ~ and ^

You rarely type full hashes. Git understands relative names:

NameMeans
HEADthe commit you are on
HEAD~1 or HEAD^its parent (one back)
HEAD~3three commits back
main, origin/mainwhere a branch points
v1.0.0a tag
HEAD^2the second parent of a merge commit

git show: one commit in detail

git show prints a commit's message followed by its diff:

git show HEAD~2
SHELLbashutf-8 · 1L
commit c82322fd58574d608be2cf68df8d838427e08087Author: Ravi Kumar <ravi@acme.dev>Date:   Thu Nov 5 22:00:00 2026 +0000    Bump dependencies and tidy pricingdiff --git a/pricing.py b/pricing.pyindex 0facded..8dd9e47 100644--- a/pricing.py+++ b/pricing.py@@ -1,4 +1,4 @@-TAX_RATE = 0.18+TAX_RATE = 1.8   def total(subtotal):diff --git a/requirements.txt b/requirements.txtindex 466a400..42941bf 100644--- a/requirements.txt+++ b/requirements.txt@@ -1 +1,2 @@ flask==3.1.0+gunicorn==23.0.0
OUTPUTterminalutf-8 · 23L

Did you spot it? A commit described as a dependency bump also changed the tax rate from 18% to 180%. Reading diffs is how such bugs are caught, ideally in code review. You will hunt this exact bug down with git bisect later in the course.

git show HEAD:pricing.py prints a file as it was in a commit, without changing anything.

Reading a diff

diff --git a/pricing.py b/pricing.py      which file--- a/pricing.py                          the old version+++ b/pricing.py                          the new version@@ -1,4 +1,4 @@                           hunk: old lines 1-4, new lines 1-4-TAX_RATE = 0.18                           removed line+TAX_RATE = 1.8                            added line                                          unchanged context line
OUTPUTterminalutf-8 · 7L

A changed line is shown as one removal and one addition. Unchanged lines around a change give context (three by default).

git diff: comparing states

git diff compares two of the three areas, or any two commits:

CommandCompares
git diffstaging area vs working tree: changes not yet staged
git diff --stagedlast commit vs staging area: what will be committed
git diff HEADlast commit vs working tree: everything since the last commit
git diff A Bcommit A vs commit B
git diff A B -- filethe same, for one file
git diff --statsummary instead of full diff

Watch a change move from unstaged to staged:

printf 'flask==3.1.0\ngunicorn==23.0.0\nrequests==2.32.3\n' > requirements.txtgit diffgit add requirements.txtgit diffgit diff --staged
SHELLbashutf-8 · 5L
diff --git a/requirements.txt b/requirements.txtindex 42941bf..2068bc5 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,2 +1,3 @@ flask==3.1.0 gunicorn==23.0.0+requests==2.32.3diff --git a/requirements.txt b/requirements.txtindex 42941bf..2068bc5 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,2 +1,3 @@ flask==3.1.0 gunicorn==23.0.0+requests==2.32.3
OUTPUTterminalutf-8 · 16L

After git add, plain git diff printed nothing: there are no unstaged changes left. The change now shows in git diff --staged. Running git diff --staged right before every commit is an excellent habit.

Compare a release with today:

git diff v1.0.0 HEAD --stat
SHELLbashutf-8 · 1L
 .gitlab-ci.yml   |   7 +++++++ Dockerfile       |   5 +++++ app.py           |   9 ++++++++- pricing.py       |   2 +- requirements.txt |   1 + 5 files changed, 22 insertions(+), 2 deletions(-)
OUTPUTterminalutf-8 · 6L

That is a release note in one command: everything that changed since v1.0.0.

Who wrote the most?

git shortlog -sn
SHELLbashutf-8 · 1L
     4	Priya Sharma     3	Ravi Kumar     2	Leela Kumar
OUTPUTterminalutf-8 · 3L

Real-world scenario: what did we deploy?

A deploy at 14:00 increased error rates. The pipeline logs show it deployed commit ce797e5, and the previous deploy was v1.0.0. The on-call engineer runs git log --oneline v1.0.0..ce797e5 to list every change in the release, then git diff v1.0.0 ce797e5 -- pricing.py on the only file related to the failing checkout endpoint. The bad tax-rate line is visible within two minutes, without opening a single file by hand.

Common mistakes

  • Expecting git diff to show staged changes: use git diff --staged.
  • Reading only commit messages: messages can be wrong or incomplete, as the "tidy pricing" commit shows. Read the diff.
  • Getting stuck in the pager: long output opens in less; press q to quit, /word to search.
  • Confusing HEAD~2 (two back on the first-parent line) with HEAD^2 (second parent of a merge).

Practice

  1. Show only the last three commits as one line each.
  2. List the commits that changed app.py, and view the oldest one with git show.
  3. Find every commit by Priya.
  4. Print requirements.txt as it was at v1.0.0 with git show v1.0.0:requirements.txt.
  5. Change README.md, then compare the output of git diff, git diff --staged and git diff HEAD before and after staging.

Cheat sheet

git log [--oneline] [-n N] [--graph --all]git log --author=X  --grep=word  --since="1 week ago"git log -- path            history of a filegit log --stat | -p        files changed | full diffsgit log --format="%h %an %ar %s"git log A..B               commits in B that are not in Agit show <commit>          one commit with its diffgit show <commit>:path     a file at that commitgit diff                   unstaged changesgit diff --staged          staged changesgit diff HEAD              all changes since last commitgit diff A B [-- path] [--stat]git shortlog -sn           commits per author
OUTPUTterminalutf-8 · 13L

Key takeaways

  • git log browses history; filter it by author, message, date or file, and use --oneline --graph for an overview.
  • HEAD, HEAD~n, branch names and tags are easier names for commits than hashes.
  • git show displays one commit; git show commit:path displays a file at that point.
  • git diff compares working tree, staging area and commits; check git diff --staged before committing.
  • Always read the diff, not only the message.