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 -5Filtering 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"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~2Comparing 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
Exercise: Git History
What does `git log` display by default?