Git

History & Inspection

Git · 10 entries

Compact Log

syntax
git log --oneline [-n]
example
git log --oneline -5
output
a1b2c3d Add user authentication endpoint
f4e5d6c Update API response format
9g8h7i6 Fix cart total calculation
b3c4d5e Add product search
1e2f3g4 Initial commit

Note Shows each commit on one line with abbreviated hash and message. Add -n to limit the number of results.

Visual Branch Graph

syntax
git log --oneline --graph --all
example
git log --oneline --graph --all --decorate
output
* a1b2c3d (HEAD -> main) Merge feature/payments
|\  
| * d4e5f6g Add payment processing
| * h7i8j9k Add payment form
|/  
* 9g8h7i6 Update dependencies
* b3c4d5e Initial commit

Note This is the CLI equivalent of a graphical Git viewer. --all shows all branches, --decorate labels commits with branch/tag names. A great alias candidate: git config --global alias.lg 'log --oneline --graph --all --decorate'

See Who Changed Each Line

syntax
git blame <file> [-L start,end]
example
git blame src/auth.js
git blame -L 10,25 src/auth.js
output
a1b2c3d (Samira Khan  2026-03-30 14:22) const validateToken = (token) => {
d4e5f6g (Dev Patel    2026-03-28 09:15)   if (!token) return null;
a1b2c3d (Samira Khan  2026-03-30 14:22)   return jwt.verify(token, SECRET);

Note Shows the commit, author, date, and line content for each line. Use -L to limit to a range. Blame doesn't mean assigning fault — it's just finding who last changed a line and why.

Binary Search for Bug Introduction

syntax
git bisect start
git bisect bad [commit]
git bisect good <commit>
example
git bisect start
git bisect bad          # Current commit is broken
git bisect good v1.0.0  # This tag was working

# Git checks out a middle commit, you test it:
# If it's good:
git bisect good
# If it's bad:
git bisect bad
# Repeat until Git finds the first bad commit

git bisect reset  # Return to original branch
output
Bisecting: 8 revisions left to test after this (roughly 3 steps)
a1b2c3d is the first bad commit

Note Bisect uses binary search to find the exact commit that introduced a bug. For N commits, it takes at most log2(N) steps. You can automate it with: git bisect run <test-script>

View Reference Log (Recovery Tool)

syntax
git reflog [branch]
example
git reflog
git reflog show feature/payments
output
a1b2c3d HEAD@{0}: commit: Add auth endpoint
f4e5d6c HEAD@{1}: reset: moving to HEAD~1
9g8h7i6 HEAD@{2}: commit: Broken attempt
b3c4d5e HEAD@{3}: checkout: moving from feature to main

Note The reflog records every time HEAD moves. It's your safety net — even after a hard reset or accidental branch deletion, you can find the commit hash here and recover with: git reset --hard HEAD@{n}. Entries expire after ~90 days.

Diff Between Two Branches

syntax
git diff <branch1>..<branch2>
git diff <branch1>...<branch2>
example
# All differences between the two branches:
git diff main..feature/payments

# Only changes introduced in feature/payments since it diverged from main:
git diff main...feature/payments

Note Two dots (..) shows the full diff between branch tips. Three dots (...) shows only what changed in the right branch since the common ancestor. Three dots is usually what you want for reviewing a feature branch.

Cherry-Pick a Commit

syntax
git cherry-pick <commit> [<commit2> ...]
example
git switch main
git cherry-pick a1b2c3d

# Cherry-pick a range:
git cherry-pick d4e5f6g..h7i8j9k
output
[main x1y2z3w] Add user authentication endpoint
 2 files changed, 45 insertions(+), 3 deletions(-)

Note Creates a NEW commit with the same changes (but different hash). Useful for applying a bugfix from one branch to another without merging the whole branch. If there's a conflict, resolve it and run git cherry-pick --continue.

Search Commit Messages & Content

syntax
git log --grep="<pattern>"
git log -S "<string>"
git log -G "<regex>"
example
# Find commits mentioning 'authentication':
git log --grep="authentication" --oneline

# Find commits where 'validateToken' was added or removed:
git log -S "validateToken" --oneline

# Regex search in diffs:
git log -G "api.*endpoint" --oneline
output
a1b2c3d Add user authentication endpoint
f4e5d6c Refactor authentication middleware

Note -S (pickaxe) finds commits where the number of occurrences of a string changed. -G finds commits where the diff matches a regex. --grep searches commit messages only.

View History of a Specific File

syntax
git log [--follow] -- <file>
example
git log --oneline -- src/auth.js
git log --follow --oneline -- src/auth.js
output
a1b2c3d Add user authentication endpoint
f4e5d6c Refactor auth module
9g8h7i6 Initial auth setup

Note Use --follow to track the file across renames. Without it, Git stops at the rename point. Add -p to see the actual diff for each commit.

Custom Log Format

syntax
git log --pretty=format:"<placeholders>"
example
git log --pretty=format:"%h %an %ar %s" -10
output
a1b2c3d Samira Khan 2 days ago Add auth endpoint
f4e5d6c Dev Patel 5 days ago Update API format
9g8h7i6 Aiko Tanaka 1 week ago Fix cart bug

Note Common placeholders: %h (short hash), %H (full hash), %an (author name), %ae (author email), %ar (relative date), %s (subject), %b (body). %C(color) adds color — e.g. %C(yellow)%h%C(reset).