Stage 2 · Tools
Git Fundamentals
log, diff, status
Navigate history, compare changes, and understand your working tree state.
git log
git log shows the commit history. By default it displays commits in reverse chronological order with hash, author, date, and message. The real power is in the flags that shape the output.
git log --oneline --graph --all--oneline collapses each commit to one line. --graph draws the branch/merge topology. --all shows commits from every branch. This is the single most useful log command for understanding project history.
# Last 5 commits
git log -5
# Commits by a specific author
git log --author="Alice"
# Commits that touch a specific file
git log -- src/auth.ts
# Commits mentioning a keyword
git log --grep="fix"
# Commits after a date
git log --after="2026-01-01"
# Range between two commits
git log abc1234..def5678These filters combine with each other. git log --author='Alice' --after='2026-01-01' -- src/auth.ts shows Alice's commits to src/auth.ts since January 1st.
Pipe long log output through a pager: git log --oneline --graph --all | less. Press j/k to scroll, q to quit. Git uses a pager by default — configure it with git config --global core.pager.
Formatting Log Output
The --pretty=format flag lets you define exactly what each log line looks like using placeholders like %h (short hash), %s (subject), %an (author name), and %ar (relative date).
git log --pretty=format:"%C(yellow)%h%C(reset) %C(blue)%ar%C(reset) %C(green)%an%C(reset) %s" -10Color codes use %C(color) syntax. This format shows: short hash in yellow, relative time in blue, author in green, then the subject. Customize to match your team's preferences.
git log --pretty=format:"%h|%s|%an|%ad" --date=short -10Pipe-delimited output is easy to parse with awk, cut, or any scripting language. Useful for generating changelogs or filtering commits programmatically.
git diff
git diff shows the differences between states. Without arguments, it compares the working tree to the staging area. With --staged, it compares the staging area to the last commit.
# Working tree vs staging area (unstaged changes)
git diff
# Staging area vs last commit (staged changes)
git diff --staged
# Working tree vs last commit (everything)
git diff HEADThis maps directly to the three states: working tree → staging area → repository. Use git diff for changes you have not staged yet. Use git diff --staged to review what you are about to commit.
# Show 3 lines of context (default is 3)
git diff -U5
# Show only filenames that changed
git diff --name-only
# Show only stats (files changed, insertions, deletions)
git diff --stat
# Word-level diff instead of line-level
git diff --word-diff--word-diff highlights individual word changes within a line, which is useful for spotting small changes in long lines. --stat gives a quick overview of which files changed and by how much.
Diffing Branches
You can diff any two commits, branches, or tags. This is essential for reviewing changes before merging or for understanding what a release introduced.
# Compare two branches
git diff main..feature-login
# Compare the last 3 commits on current branch
git diff HEAD~3..HEAD
# Compare a branch to its merge base with main
git diff main...feature-login
# Compare tags
git diff v1.0.0..v2.0.0The two-dot .. compares two endpoints directly. The three-dot ... compares from the merge base (common ancestor) to the target — this shows only the changes on the feature branch, excluding commits already on main.
A..B shows everything in B that is not in A (the symmetric difference). A...B shows everything reachable from B but not from A, starting from their common ancestor. Use two dots for direct comparison, three dots for feature-branch review.
git status -s
The short format (-s or --short) gives a compact two-column view of your working tree state. The left column shows the staging area state, the right column shows the working tree state.
git status -s
# M README.md — modified, not staged
# A app.js — staged (new file)
# M config.json — staged modification
# D old-file.txt — staged deletion
# ?? temp.log — untracked file
#!! .DS_Store — ignored fileColumn meanings: M = modified, A = added, D = deleted, R = renamed, C = copied, ?? = untracked, !! = ignored. Two columns show the staging vs working tree distinction at a glance.
| Status Code | Staging Area | Working Tree | Meaning |
|---|---|---|---|
M | Modified | Same | Changes are staged |
M | Same | Modified | Changes are unstaged |
MM | Modified | Modified again | Staged, then more changes made |
A | Added | Same | New file staged |
?? | Not tracked | Not tracked | Untracked file |
Run git config --global alias.st status -s so you can type git st instead. Short aliases compound: git config --global alias.co checkout, git config --global alias.br branch.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.