Git History

Git's history tools, log, show, and diff, let you inspect not just what changed, but when, why, and by whom, at whatever level of detail you need.

Reading the Commit Log

git log walks backward from your current commit through its ancestry, newest first, printing the hash, author, date, and message of each commit. On its own it's verbose; a handful of flags make it dramatically more useful day to day.

Compact log output

$ git log --oneline -5

Filtering and Formatting Log Output

Beyond --oneline, git log accepts filters for author and date range, plus a --graph flag that draws the branch and merge topology in ASCII alongside each entry, invaluable once a repository has more than a couple of branches merging regularly.

Filter by author and draw the graph

$ git log --oneline --graph --author="Priya" --since="2 weeks ago"
FlagPurpose
--onelineOne line per commit: short hash plus subject
--graphASCII graph of branch and merge topology
-p or --patchFull diff for each commit
--statSummary of files changed and line counts
-- <path>Restrict history to commits touching that path
--since / --untilRestrict to a date range

Inspecting a Single Commit

git show <commit> displays one commit in full: its metadata plus the diff it introduced. It accepts the same references as everything else in Git, a hash, a branch name, HEAD, or HEAD~2, so you rarely need to copy full 40-character hashes around.

Show a specific commit

$ git show HEAD~2

Comparing with Diff

Where show looks at one commit, git diff compares two arbitrary points: the working directory against the index, the index against HEAD, or two branches or commits directly. Understanding which two things you're comparing is the whole trick to using it correctly.

  • git diff - working directory vs the staging area (index)
  • git diff --staged - staging area vs the last commit
  • git diff main..feature/nav - tip of main vs tip of feature/nav
  • git diff HEAD~3 HEAD - the last three commits combined
  • git diff -- path/to/file.js - restrict the comparison to one file
Note: git log -p -- path/to/file.js gives you the full history of just one file, diff and all, often faster than a blame view when you want to understand how a file evolved.
Note: Hashes are content-addressed, not sequential. Two developers who make an identical commit, same tree, parent, message, and timestamp, get the identical hash. That's a feature, not a coincidence.

Exercise: Git History

What does `git log` display by default?