The Hook (The "Byte-Sized" Intro)
When history gets noisy, a cheat sheet keeps you fast. Here's every Git history command you need — organized by the question you're trying to answer. Print it. Pin it. Never google "how to find who changed this line" again.
📖 History Search Quick Reference
🔍 "What happened?"
| Question | Command |
|---|
| Recent commits | git log --oneline -10 |
| Graph view | git log --oneline --graph --all |
| Changes in a file | git log --oneline -- file.js |
| Changes between releases | git log v1.0..v2.0 --oneline |
| Skip merge commits | git log --no-merges --oneline |
👤 "Who did this?"
| Question | Command |
|---|
| Who wrote this line? | git blame file.js |
| Blame specific lines | git blame -L 10,20 file.js |
| Ignore whitespace | git blame -w file.js |
| Track moved code | git blame -C file.js |
| Filter by author | git log --author="name" |
📅 "When did this happen?"
| Question | Command |
|---|
| Since a date | git log --since="2026-01-01" |
| Last 2 weeks | git log --since="2 weeks ago" |
| Between dates | git log --since="Jan 1" --until="Feb 1" |
| When code was added | git log -S "code" --oneline |
🐛 "What introduced this bug?"
| Question | Command |
|---|
| Binary search | git bisect start HEAD v1.0 |
| Mark good | git bisect good |
| Mark bad | git bisect bad |
| Automate | git bisect run npm test |
| End bisect | git bisect reset |
🔎 "Where is this code?"
| Question | Command |
|---|
| Search tracked files | git grep "term" |
| With line numbers | git grep -n "term" |
| Files only | git grep -l "term" |
| Count per file | git grep -c "term" |
| At a specific version | git grep "term" v1.0 |
| In specific file types | git grep "term" -- "*.js" |
📊 "What changed between X and Y?"
| Question | Command |
|---|
| Diff two commits | git diff A..B |
| File summary | git diff A..B --stat |
| Changed filenames | git diff --name-only A..B |
| From fork point | git diff A...B |
| Commits in B not A | git log A..B --oneline |
🔧 "Low-level inspection"
| Question | Command |
|---|
| Object type | git cat-file -t SHA |
| Object content | git cat-file -p SHA |
| Full SHA from short | git rev-parse SHORT |
| Closest tag | git describe |
| Dangling objects | git fsck --unreachable |
Key Takeaways
- Organize your search by the question you're asking.
log for history, blame for attribution, bisect for bugs, grep for content.
- Combine filters for precision:
--author + --since + -- path.
- Bookmark this cheat sheet for quick reference.
Interview Prep
-
Q: What are the main Git tools for searching history?
A: git log (with filters) for commit history, git blame for line-by-line attribution, git log -S (pickaxe) for when code was added/removed, git bisect for finding bug-introducing commits, and git grep for searching current/historical file content.
-
Q: How do you combine multiple search criteria?
A: Chain filters on git log: git log --author="name" --since="date" --grep="keyword" -- path/to/file. Filters are ANDed together for precise results.
-
Q: What is the fastest way to find a regression's root cause?
A: git bisect run <test-script>. Provide a known-good and known-bad commit, and Git automatically binary-searches through the history, running your test at each midpoint. It finds the first bad commit in O(log n) steps.