By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
git log
A hyper-practical, zero-fluff guide for engineers who need to debug, audit, or recover code in production.
git log is your time machine for code. It lets you: - Debug regressions ("When did this bug get introduced?") - Audit changes ("Who modified this security-sensitive file?") - Recover lost work ("I accidentally deleted a branch—how do I get it back?") - Understand context ("Why was this hacky fix merged in the first place?")
Real-world scenario:You’re a DevOps engineer oncall at 2 AM. A critical service starts failing after a deploy. The last commit message says "fix: typo", but the diff is 200 lines long. You need to: 1. Find the exact commit that broke things.2. See the full changes (not just the summary).3. Compare branches to identify what’s different in production vs. staging.4. Recover a deleted branch that had the working code.
"fix: typo"
If you don’t master git log, you’ll waste hours digging through GitHub’s UI or manually diffing files. Worse, you might permanently lose work because you didn’t know git reflog exists.
git reflog
--oneline
--graph
<short-hash> <commit-message>
"fix bug"
"fix: race condition in user auth (issue #123)"
--patch
-p
--stat
--grep=<pattern>
--grep="fix"
--grep="auth"
--author=<name>
--since
--until
--since="2 weeks ago"
git reset --hard
reflog
git show <commit>
git show
bash git clone https://github.com/your-username/your-repo.git cd your-repo
git commit
git branch
git checkout
Scenario: A test starts failing in CI. You suspect it’s related to a recent change in auth.js.
auth.js
git log --oneline -n 10
Output:
a1b2c3d (HEAD -> main) fix: typo in login form e4f5g6h feat: add OAuth support i7j8k9l refactor: extract auth logic ...
git log --oneline -- auth.js
e4f5g6h feat: add OAuth support i7j8k9l refactor: extract auth logic
git show e4f5g6h
commit e4f5g6h...Author: Alice <[email protected]> Date: Mon Oct 2 10:00:00 2023 +0000 feat: add OAuth support diff --git a/auth.js b/auth.js index abc123..def456 100644 --- a/auth.js +++ b/auth.js @@ -10,6 +10,12 @@ function login() { + if (useOAuth) { + // New OAuth logic + return oauthLogin(); + } }
✅ Found it! The bug is in the new OAuth logic.
Scenario: You’re debugging a merge conflict. You need to see how feature/login diverged from main.
feature/login
main
git log --oneline --graph --all
* a1b2c3d (HEAD -> main) fix: typo in login form | * e4f5g6h (feature/login) feat: add OAuth support | * i7j8k9l refactor: extract auth logic |/ * d3e2f1g chore: update dependencies
? Insight: feature/login branched off after d3e2f1g and has 2 new commits.
d3e2f1g
git log main..feature/login --oneline
Scenario: You accidentally deleted feature/login and need to restore it.
a1b2c3d (HEAD -> main) HEAD@{0}: checkout: moving from feature/login to main e4f5g6h (feature/login) HEAD@{1}: commit: feat: add OAuth support i7j8k9l HEAD@{2}: checkout: moving from main to feature/login ...
git branch feature/login e4f5g6h
✅ Done! The branch is back.
Scenario: Security asks, "Who modified config/secrets.yml in the last month?"
config/secrets.yml
git log --oneline --since="1 month ago" -- config/secrets.yml
a1b2c3d (HEAD -> main) fix: rotate API keys e4f5g6h feat: add AWS credentials
git show a1b2c3d
commit a1b2c3d...Author: Bob <[email protected]> Date: Tue Oct 3 14:00:00 2023 +0000 fix: rotate API keys diff --git a/config/secrets.yml b/config/secrets.yml index 123abc..456def 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -1,3 +1,3 @@ api_key: "old_key_123" -db_password: "password123" +db_password: "new_password_456"
? Insight: Bob rotated the DB password (good), but the old key is still in the history (bad—this is a security risk).
git filter-repo
--no-merges
--depth=1
bash git remote prune origin git gc --aggressive
git tag -a v1.0 -m "Release v1.0"
git log --first-parent
git log --since="1 hour ago" | grep -v "CI"
git reflog show --date=iso
git lg
git fsck
git log -p
git branch -D
git log -p -- <file>
git bisect
Trap: git log alone won’t show the diff—you need -p.
"How do you recover a deleted branch?"
git branch <name> <commit>
Trap: git log won’t show deleted branches—only reflog will.
"How do you visualize branch history?"
Trap: --graph is required to see merges.
"How do you filter commits by author?"
git log --author="Alice"
Trap: --grep searches messages, not authors.
--grep
"How do you see what changed in the last week?"
git log --since="1 week ago" --stat
"2 days ago"
Challenge:You’re reviewing a PR and notice a suspicious commit (abc123) with the message "fix: typo". The diff is 300 lines long. How do you: 1. See only the changes in this commit (no noise)? 2. Find who approved this commit in GitHub?
abc123
Solution:1. git show abc123 (shows the full diff).2. Check GitHub’s PR timeline or run git log --pretty=format:"%h %an %s" | grep abc123 to see the author.
git show abc123
git log --pretty=format:"%h %an %s" | grep abc123
Why it works:- git show gives the exact diff of a commit.- GitHub’s UI (or git log --pretty=format) reveals the author.
git log --pretty=format
git log --oneline
git log --oneline -n 5
git log --graph --all
git log -p -- auth.js
git log --stat
git log --stat --since="1 week ago"
git log --grep="fix"
git log --grep="security"
git log --author="[email protected]"
git log main..feature
git log main..feature/login
git log --since="2 days ago"
git log --since="1 month ago"
git reflog | grep "checkout: moving"
--pretty=format:"%h %s"
git log --pretty=format:"%h - %an, %ar : %s"
log
filter-repo
Alias your most-used git log commands in ~/.gitconfig:
~/.gitconfig
[alias] lg = log --oneline --graph --all lga = log --oneline --graph --all --author="$(git config user.email)" lgs = log --oneline --stat lgp = log -p
Now you can run git lg instead of typing the full command. Save keystrokes, save time.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.