G
GuideDevOps
Lesson 6 of 18

Git Log & History

Part of the Git & Version Control tutorial series.

Basic Git Log

Simple Log View

git log
# Shows all commits, newest first
# Press 'q' to exit

Output shows:

  • commit hash (SHA-1)
  • author and date
  • commit message

One-Line View

git log --oneline
# Much more compact:
# abc1234 Fix login bug
# def5678 Add search feature

Limit Number of Commits

git log -5          # Last 5 commits
git log -n 10       # Last 10 commits

Filtering By Details

Filter by Author

git log --author="John"
git log --author="[email protected]"
 
# Multiple authors
git log --author="John.*|Jane"

Filter by Commit Message

# Case-insensitive search
git log --grep="bug"
 
# Case-sensitive
git log --grep="Bug" -i
 
# Combine multiple patterns
git log --grep="fix" --grep="error"

Filter by Date

git log --since="2024-01-01"
git log --until="2024-12-31"
 
git log --after="2 weeks ago"
git log --before="3 days ago"
 
# Relative dates
git log --since="1 month ago"
git log --until="yesterday"

Filter by File

# Commits that modified specific file
git log -- app/routes.ts
 
# Commits that modified multiple files
git log -- app/ src/
 
# Commits that deleted/added file
git log --follow -- original_name.ts

Advanced Formatting

Custom Format

# Show statistics on changes
git log --stat
 
# Show files changed in each commit
git log --name-status
 
# Show diff for each commit
git log -p
# Shows full diff, use 'q' to navigate

Simple Custom Format

# One-line with graph
git log --graph --oneline
 
# Better multi-branch view
git log --graph --oneline --all
 
# More detailed
git log --pretty=format:"%h - %an - %ar : %s"
# %h = hash, %an = author, %ar = date (relative), %s = subject

Decorate

# Show branch/tag info
git log --oneline --decorate
 
# All branches and remotes
git log --oneline --decorate --all

Searching Commits

Find Specific Changes

# Find commits that added/removed specific text
git log -S "searchTerm"
git log -S "auth" -p  # Show the diff too
 
# Find commits with specific string in diff
git log -G "searchTerm"

Find Bad Commit (Bisect)

# Start git bisect
git bisect start
 
# Mark current as bad
git bisect bad
 
# Mark known good commit
git bisect good abc1234
 
# Bisect will check out commits to binary search
# After each test:
git bisect good    # This commit is good
git bisect bad     # This commit is bad
 
# Find it!
git bisect reset   # Return to original branch

Comparing Commits and Branches

Diff Between Commits

git show abc1234     # View specific commit
 
# Diff between two commits
git diff abc1234 def5678
 
# Diff between branches
git diff main feature/auth

Log Between Branches

# Commits in feature but not in main
git log main..feature/auth
 
# Commits in main but not feature
git log feature/auth..main
 
# Commits in both (merge base)
git log --merge

Viewing Commit Details

Full Commit Information

# View specific commit
git show abc1234
 
# Show metadata only
git cat-file -p abc1234

Show File at Specific Commit

# View old version of file
git show abc1234:path/to/file.ts
 
# Compare old vs current
git diff abc1234 -- path/to/file.ts

Useful Aliases

Add to ~/.gitconfig:

[alias]
    lg = log --graph --oneline --all --decorate
    history = log -p
    authors = shortlog --all --summary --count
    changes = diff --name-only
    timeline = log --reverse --oneline

Then use:

git lg          # Nice graph view
git authors     # See all contributors

Common Workflows

Find When Bug Was Introduced

git log -p -- app/user.ts | grep -B5 "bug"
# Search through commits for your bug
 
# Or use bisect:
git bisect start
git bisect bad          # Current version is broken
git bisect good v1.0    # Known good version
# Binary search to find exact commit

Get Commit Statistics

# Lines changed per author
git log --pretty=format:"%an" --numstat | awk '{print $3}' | sort | uniq -c
 
# Most active days
git log --pretty=format:"%aI" | cut -d'T' -f1 | sort | uniq -c | sort -rn | head

Review Before Merge

# See all changes in feature branch
git log --oneline main..feature/auth
git diff main..feature/auth

Tips

✓ Use --oneline for quick overview
✓ Use -p to see actual changes
✓ Filter by date to find recent changes
✓ Use --grep to find commits by message
git show commit-hash for specific detail
git bisect to binary-search for buggy commit

title: "Git Log & History" description: "Inspecting the past: How to read the audit trail." order: 6

Standard Log

```bash git log ```

Prettier Logs (Useful for DevOps)

```bash

One-liner with graph

git log --oneline --graph --all --decorate

See only the last 5 commits

git log -n 5

See changes by a specific author

git log --author="Jane Doe" ```

Inspecting a Specific Commit

```bash git show ``` This shows the "diff" of exactly what changed in that snapshot.