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 shopIn 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 2commit 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 /healthEach 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 --onelinece797e5 (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 commitThe first seven characters of a hash are almost always enough to identify a commit, so you can type git show c82322f.
Filtering history
| Option | Shows |
|---|---|
-n 5 or -5 | only the last 5 commits |
--author=Ravi | commits by an author (partial match) |
--grep=tax | commits whose message contains a word |
--since="2 weeks ago" | commits in a time range (--until too) |
-- path/file | commits that changed a file or folder |
--stat | which files changed and by how much |
-p | the full diff of each commit |
--graph --all | branches and merges as a drawing, all branches |
git log --oneline --author=Ravigit log --oneline -- pricing.pygit log --stat -n 1c82322f 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(+)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 3ce797e5 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 pricingNaming commits: HEAD, ~ and ^
You rarely type full hashes. Git understands relative names:
| Name | Means |
|---|---|
HEAD | the commit you are on |
HEAD~1 or HEAD^ | its parent (one back) |
HEAD~3 | three commits back |
main, origin/main | where a branch points |
v1.0.0 | a tag |
HEAD^2 | the 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~2commit 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.0Did 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 lineA 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:
| Command | Compares |
|---|---|
git diff | staging area vs working tree: changes not yet staged |
git diff --staged | last commit vs staging area: what will be committed |
git diff HEAD | last commit vs working tree: everything since the last commit |
git diff A B | commit A vs commit B |
git diff A B -- file | the same, for one file |
git diff --stat | summary 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 --stageddiff --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.3After 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 .gitlab-ci.yml | 7 +++++++ Dockerfile | 5 +++++ app.py | 9 ++++++++- pricing.py | 2 +- requirements.txt | 1 + 5 files changed, 22 insertions(+), 2 deletions(-)That is a release note in one command: everything that changed since v1.0.0.
Who wrote the most?
git shortlog -sn 4 Priya Sharma 3 Ravi Kumar 2 Leela KumarReal-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 diffto show staged changes: usegit 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; pressqto quit,/wordto search. - Confusing
HEAD~2(two back on the first-parent line) withHEAD^2(second parent of a merge).
Practice
- Show only the last three commits as one line each.
- List the commits that changed
app.py, and view the oldest one withgit show. - Find every commit by Priya.
- Print
requirements.txtas it was atv1.0.0withgit show v1.0.0:requirements.txt. - Change
README.md, then compare the output ofgit diff,git diff --stagedandgit diff HEADbefore 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 authorKey takeaways
git logbrowses history; filter it by author, message, date or file, and use--oneline --graphfor an overview.HEAD,HEAD~n, branch names and tags are easier names for commits than hashes.git showdisplays one commit;git show commit:pathdisplays a file at that point.git diffcompares working tree, staging area and commits; checkgit diff --stagedbefore committing.- Always read the diff, not only the message.