View Git History and Changes: git log, git show, git diff and git blame

Git remembers everything, but only if you know how to ask. In this lesson you will explore history with git log, inspect commits with git show, compare versions with git diff and find who last changed a line with git blame.

Advertisement

📖 What You’ll Learn: git log and its most useful options, custom log formats, git show and commit references like HEAD~1, git diff between commits, files and branches, how to read a diff, git blame, and history on GitHub.


📜 Why History Matters

Git’s history answers the questions every team asks: What changed? Who changed it? When? Why? In this lesson you will learn to read history with git log, inspect a single commit with git show, compare versions with git diff and find who last changed a line with git blame.

Use your git-practice repository from your first repository, with a few commits in it.


📖 git log: The Commit History

git log
commit 8b1d4e2c7a9f0b1e2d3c4b5a69788f7e6d5c4b3a (HEAD -> main)
Author: Asha Verma <asha.verma@example.com>
Date:   Thu Oct 30 10:15:42 2026 +0530

    Add learning goals and initial test plan

commit 3f2a91c...

Each entry shows the full commit hash, author, date and message. Newest commits are at the top. Press q to exit the pager and Space to scroll.

The most useful git log options

CommandShows
git log --onelineOne line per commit: short hash and message
git log --oneline --graph --all --decorateBranches and merges as a text graph
git log -n 5Only the last 5 commits
git log --statFiles changed and lines added/removed per commit
git log -pThe full diff of every commit
git log --author="Asha"Commits by an author
git log --since="2 weeks ago"Commits in a time range (also --until)
git log --grep="login"Commits whose message contains “login”
git log -- tests/login.spec.tsCommits that changed one file
git log -S "waitForTimeout"Commits that added or removed that text

💡 Tip: Create the alias git lg from the config lesson: git config --global alias.lg "log --oneline --graph --decorate --all". You will use it constantly once you work with branches.

Custom formats

git log --pretty=format:"%h %an %ar %s"
8b1d4e2 Asha Verma 2 hours ago Add learning goals and initial test plan
3f2a91c Asha Verma 3 hours ago Add README with project title

Common placeholders: %h short hash, %an author name, %ar relative date, %ad date, %s subject.


🔍 git show: Inspect One Commit

git show 8b1d4e2          # a specific commit (the short hash is enough)
git show HEAD             # the latest commit
git show HEAD~1           # the commit before it
git show HEAD:README.md   # a file's content as it was in that commit
git show --stat HEAD      # only the list of changed files

Referring to commits

ReferenceMeaning
HEADThe commit you are on now
HEAD~1 or HEAD^One commit before HEAD
HEAD~3Three commits before HEAD
mainThe latest commit on the main branch
8b1d4e2A specific commit by its (short) hash

âš–ī¸ git diff: Compare Versions

git diff                       # unstaged changes
git diff --staged              # staged changes (what you are about to commit)
git diff HEAD~2 HEAD           # changes between two commits
git diff HEAD~2 -- README.md   # one file only
git diff main feature/login    # two branches
git diff --stat main feature/login   # summary only
git diff --word-diff           # highlight changed words instead of whole lines

Reading a diff

diff --git a/test-plan.txt b/test-plan.txt
index 3b18e51..a5c1966 100644
--- a/test-plan.txt
+++ b/test-plan.txt
@@ -1,2 +1,3 @@
 Smoke tests: login, search, checkout
-API tests: none
+API tests: users, orders
+Visual tests: home page
  • --- is the old version, +++ the new version.
  • @@ -1,2 +1,3 @@ means “from line 1, 2 lines before; from line 1, 3 lines after”.
  • Lines starting with - were removed, + were added, and lines with a space are unchanged context.

📝 Note: The staging-area variants of git diff are explained in Working Directory, Staging Area and Repository.


đŸ•ĩī¸ git blame: Who Changed This Line?

git blame tests/login.spec.ts
git blame -L 10,20 tests/login.spec.ts   # only lines 10 to 20
8b1d4e2c (Asha Verma 2026-10-30 10:15:42 +0530 12)   await page.getByRole('button', { name: 'Login' }).click();

Each line shows the commit, author and date of its last change. Despite the name, use it to find context, not to blame people. In VS Code, extensions such as GitLens show this inline, and GitHub shows it with the Blame button on any file.


🌐 History on GitHub

On github.com, every repository has a commits link with the full history, each commit has a diff view, and every file has History and Blame buttons. These are the same commands, with a friendly interface.


📝 Assignment

  1. Run git log, git log --oneline and git log --stat and describe the difference.
  2. Show only commits that changed README.md.
  3. Use git show HEAD~1 and git show HEAD:README.md and explain what each displays.
  4. Compare your first and latest commits with git diff <first-hash> HEAD.
  5. Find the commit that added a specific word with git log -S "word".
  6. Run git blame on a file with several commits.
  7. Bonus: build a custom --pretty=format that shows the date, author and message.

❓ FAQ

🤔 How do I see commit history in Git?

Run git log, or git log --oneline for a compact view and git log --oneline --graph --all to include branches.

🤔 How do I see the changes in a specific commit?

git show <commit-hash> shows the commit’s message and full diff.

🤔 What is the difference between HEAD~1 and HEAD^?

For normal commits they are the same: the parent of HEAD. They differ only for merge commits, where ^2 means the second parent.

🤔 How do I compare two branches in Git?

git diff branch1 branch2, or git diff --stat branch1 branch2 for a summary of changed files.

🤔 How do I exit git log?

Press q to quit the pager.


đŸŽ¯ Next Steps

đŸŽŦ Learn Git and GitHub step by step: watch the series on YouTube!

Subscribe to SqaToolsByDY • Practice on sqatools.in

Leave a Comment