Skip to main content

Viewing History

  • git log → Show commit history
  • git log --oneline → Compact commit history
  • git log --graph --oneline --decorate → Visualize branch history
  • git show <commit> → Show details of a commit
  • git diff → Show unstaged changes
  • git diff --staged → Show staged changes

git log

git log [options] [<revision range>] [--] [<path>...]
# Basic log:
git log

# Limit number of commits:
git log -n 5

# Show one-line summaries:
git log --oneline
# Show graph of commits:
git log --graph --oneline --decorate

# Filter by author:
git log --author="Alice"

# Filter by date:
git log --since="2024-01-01" --until="2024-12-31"

# Show commits affecting a file:
git log -- path/to/file.txt

# Show commit stats (files changed, insertions/deletions):
git log --stat

# Show diffs with each commit:
git log -p
  • Description: The git log command is your window into a repository’s history. 
  • Usage: It shows commits in reverse chronological order (latest first), displays commit metadata: hash, author, date, and message, can be customized to show diffs, stats, or formatted output, supports filtering by branch, author, date, or file path.
  • Think Tank: Powerful filters available (--author--since).
Option Description
-n  Limit number of commits shown
--oneline Show condensed commit info (hash + message)
--graph Display ASCII graph of branch/merge history
--decorate Show branch and tag names alongside commits
--stat Show file changes and line counts per commit
-p, --patch Show diffs introduced by each commit
--author= Filter commits by author
--grep= Search commit messages by keyword
--since= / --until= Filter commits by date range
--pretty= Customize log output (e.g., short, full, format:"...")
--abbrev-commit Show shortened commit hashes
--no-merges Exclude merge commits
--reverse Show commits in chronological order (oldest first)
  • Best Practices:
    • Use --oneline --graph --decorate for quick, readable history
    • Filter logs by author, date, or file to focus on relevant changes
    • Use custom --pretty=format for scripting or automation
    • Combine with git blame or git show for deeper investigation
    • Keep commit messages clear and descriptive to make logs useful

How it works

  • Commit history: Each commit is a snapshot of the repository with metadata.
  • Revision ranges: You can specify ranges like HEAD~5..HEAD to limit output.
  • Path filtering: Restrict logs to commits affecting specific files or directories.
  • Formatting: Options allow you to tailor the output for readability or scripting.
Git - git-log Documentation

git diff

git diff [options] [<commit> [<commit>]] [--] [<path>...]
git diff --cached [options] [--] [<path>...]
git diff <commit>...<commit> [--] [<path>...]
git diff --no-index <path> <path>
# Working tree vs. index:
git diff
# Meaning: Shows unstaged changes (what you changed but haven’t added).

# Index vs. HEAD (staged changes):
git diff --cached
# Meaning: Shows what will be committed next (staged but not yet committed).

# Working tree vs. HEAD (all local changes):
git diff HEAD
# Meaning: Includes both staged and unstaged changes compared to the last commit.

# Between two commits/branches/tags:
git diff <commitA> <commitB>
git diff main feature/login
# Meaning: Shows how history differs between two points.

# Between files on disk (no repo needed):
git diff --no-index file1 file2
# Meaning: General-purpose diff outside Git, useful in scripts
  • Description: The git diff is your precise change viewer across working tree, index, and commits.
  • Usage: Shows differences between repository states: working tree vs. index, index vs. last commit, between commits/branches/tags, or even between arbitrary files. It’s your lens for reviewing changes before staging or committing.
  • Think Tank: Compare specific commits with git diff <commit1> <commit2>.
Option Description
--cached Compare index with HEAD (staged changes)
--stat Show summary of files changed and line counts
-p, --patch Show unified diff hunks (default output)
--name-only List only changed file paths
--name-status Show paths with change types (A/M/D)
--word-diff Highlight word-level changes
--color / --no-color Control colorized output
--ignore-space-change, --ignore-all-space Reduce noise from whitespace
--minimal Spend extra time to produce smaller diffs
--merge-base Diff against merge base of two commits
--no-index Diff arbitrary files outside Git
  • Best Practices:
    • Review before staging/committing: Use git diff and git diff --cached to keep commits intentional
    • Filter aggressively: Combine --stat--name-only, and pathspecs to focus on relevant changes
    • Handle text formats wisely: Use --word-diff for documentation and data files
    • Tame whitespace: Apply --ignore-space-change when diffs are noisy
    • Compare against merge base: Prefer --merge-base for feature vs. main to highlight true integration changes
Git - git-diff Documentation

Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 19, 2025