Git History
Understanding and viewing project history is an important skill in Git. This chapter details how to view, analyze, and understand Git commit history.
Basic History Viewing
git log Basics
bash
# View full commit history
git log
# View concise one-line history
git log --oneline
# Limit number of commits
git log -n 5
git log --oneline -10
# View graphical branch history
git log --graph
# Combined usage
git log --oneline --graph -10Create Demo History
Let's create a project with rich history for demonstration:
bash
# Create demo project
mkdir git-history-demo
cd git-history-demo
git init
# Create initial commit
echo "# Git History Demo Project" > README.md
git add README.md
git commit -m "Initial commit: Add README"
# Add more commits
echo "Project description" >> README.md
git add README.md
git commit -m "Update README: Add description"
echo "console.log('Hello World');" > app.js
git add app.js
git commit -m "Add main app file"
echo "body { margin: 0; }" > style.css
git add style.css
git commit -m "Add style file"Detailed Log Options
Formatting Output
bash
# Custom format
git log --pretty=format:"%h - %an, %ar : %s"
# Common format options
git log --pretty=format:"%H" # Full hash
git log --pretty=format:"%h" # Short hash
git log --pretty=format:"%an" # Author name
git log --pretty=format:"%ae" # Author email
git log --pretty=format:"%ad" # Author date
git log --pretty=format:"%ar" # Relative date
git log --pretty=format:"%s" # Commit message
git log --pretty=format:"%b" # Commit body
# Beautified log format
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)%an%C(reset), %C(blue)%ar%C(reset) : %s"Time and Date Filtering
bash
# View commits in specific period
git log --since="2 weeks ago"
git log --until="2023-12-01"
git log --since="2023-01-01" --until="2023-12-31"
# Relative time
git log --since="yesterday"
git log --since="1 week ago"
git log --since="2 months ago"
# Specific date format
git log --since="2023-01-01 00:00:00"
git log --after="Jan 1 2023" --before="Dec 31 2023"Author and Committer Filtering
bash
# Filter by author
git log --author="John Doe"
git log --author="john@example.com"
# Regex support
git log --author="John.*"
git log --author=".*@company.com"
# Filter by committer
git log --committer="Jane Doe"
# Exclude specific author
git log --author="^(?!.*John Doe).*$" --perl-regexpContent Search
bash
# Search commit messages
git log --grep="fix"
git log --grep="bug" --grep="fix" --all-match
# Search code changes
git log -S "function_name"
git log -G "regex"
# Search file content changes
git log -p -S "console.log"
# Ignore case
git log --grep="BUG" -iFile and Path Filtering
bash
# View history of specific file
git log README.md
git log -- README.md # Safer syntax
# View multiple files
git log file1.txt file2.txt
# View directory history
git log src/
git log -- src/
# View deleted file history
git log --all --full-history -- deleted_file.txtAdvanced History Viewing
Graphical History
bash
# Basic branch graph
git log --graph --oneline
# Detailed branch graph
git log --graph --pretty=format:"%C(red)%h%C(reset) -%C(yellow)%d%C(reset) %s %C(green)(%cr) %C(bold blue)<%an>%C(reset)"
# View all branches
git log --graph --oneline --all
# Simplified branch graph
git log --graph --simplify-by-decoration --oneline --allStatistics
bash
# Show change stats
git log --stat
# Show short stats
git log --shortstat
# Show changed filenames only
git log --name-only
# Show file status
git log --name-status
# Show numeric stats
git log --numstatDiff Display
bash
# Show diff for each commit
git log -p
# Limit diff context lines
git log -p -U1
# Show diff for specific file only
git log -p -- file.txt
# Show word-level diff
git log -p --word-diff