Commits are the heart of Git—they're snapshots of your project at specific points in time. Understanding how to view, navigate, and interpret your commit history is essential for effective version control. In this lesson, you'll learn how to explore your project's timeline, view detailed information about commits, compare different versions, and understand what makes each commit unique. Mastering these skills will help you track changes, debug issues, and collaborate effectively with others.
What Exactly Is a Commit?
We've already made commits, but let's understand what they really are at a deeper level.
Anatomy of a Commit
Each commit contains several key pieces of information:
- Commit Hash (SHA-1): A unique 40-character identifier (like
a1b2c3d4e5f6...) - Author Information: Name and email of who made the commit
- Date and Time: When the commit was created
- Commit Message: Description of what changed
- Parent Commit(s): Reference to the previous commit(s)
- Snapshot: Complete state of all tracked files at that moment
Commits Are Snapshots, Not Differences
This is a crucial concept: Git doesn't store the differences between versions (like "added 5 lines, removed 2"). Instead, each commit is a complete snapshot of your entire project.
Think of it like this:
- Other systems: Save the original file, then save only the changes (deltas)
- Git: Takes a full photo of everything each time, but is smart about not duplicating unchanged files
📸 Why Snapshots?
Storing snapshots makes Git faster and more reliable. You can jump to any commit instantly without having to reconstruct it from a series of changes. Git uses clever compression to make this efficient, so you don't waste disk space.
The Commit Hash (SHA-1)
Every commit gets a unique identifier called a hash. It looks like this:
a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0Key points about commit hashes:
- They're always 40 characters long (hexadecimal)
- They're generated from the commit's content using the SHA-1 algorithm
- If even one character changes in a commit, the hash is completely different
- They're globally unique—no two different commits will ever have the same hash
- You usually only need the first 7 characters to identify a commit (like
a1b2c3d)
Viewing Commit History
You've already seen git log, but it has many powerful options for viewing your history.
Basic Git Log
git logThis shows the full commit history with all details:
commit a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 (HEAD -> main)
Author: Sarah Johnson <sarah@example.com>
Date: Sat Jan 3 2026 14:30:00 +0100
Add user authentication feature
Implemented JWT-based authentication with login and logout
functionality. Added password hashing for security.
commit f4e5d6c7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3
Author: Sarah Johnson <sarah@example.com>
Date: Sat Jan 3 2026 10:15:00 +0100
Fix navigation menu overflow on mobile devices
commit c7d8e9f0a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
Author: Sarah Johnson <sarah@example.com>
Date: Fri Jan 2 2026 16:45:00 +0100
Update homepage design with new brand colorsCompact One-Line Format
For a cleaner overview, use --oneline:
git log --onelinea1b2c3d (HEAD -> main) Add user authentication feature
f4e5d6c Fix navigation menu overflow on mobile devices
c7d8e9f Update homepage design with new brand colors
b0a1c2d Initial commitThis shows just the abbreviated hash and the first line of the commit message.
Limiting Output
Show only the last few commits:
# Show last 5 commits
git log -5
# Or use -n
git log -n 3
# Combine with --oneline
git log --oneline -5Filtering by Date
# Commits from the last week
git log --since="1 week ago"
# Commits after a specific date
git log --since="2026-01-01"
# Commits before a date
git log --until="2025-12-31"
# Commits in a date range
git log --since="2 weeks ago" --until="1 week ago"Filtering by Author
# Commits by a specific author
git log --author="Sarah"
# Using full email
git log --author="sarah@example.com"Searching Commit Messages
# Find commits with "bug" in the message
git log --grep="bug"
# Case-insensitive search
git log --grep="BUG" -i
# Multiple search terms
git log --grep="auth" --grep="login"Viewing Detailed Commit Information
Show Command
The git show command displays detailed information about a specific commit, including the full diff:
# Show the most recent commit
git show
# Show a specific commit by hash
git show a1b2c3d
# Show a specific commit by relative reference
git show HEAD~1 # Show the commit before HEAD
git show HEAD~2 # Show two commits before HEADThe output includes:
- Complete commit metadata (hash, author, date, message)
- The full diff showing exactly what changed in each file
Show Specific Files
View a specific file from a commit:
# Show how a file looked in a specific commit
git show a1b2c3d:filename.txt
# Show a file from 3 commits ago
git show HEAD~3:index.htmlShow with Statistics
# Show commit with file change statistics
git show --stat
# Show commit with compact stat
git show --shortstatAdvanced Log Formatting
Include File Statistics
See which files changed and how much:
git log --statOutput shows files changed with addition/deletion counts:
commit a1b2c3d Add user authentication feature
Author: Sarah Johnson <sarah@example.com>
Date: Sat Jan 3 2026 14:30:00
Add user authentication feature
auth.js | 45 +++++++++++++++++++++++++++++++++
login.html | 12 +++++++--
package.json | 3 +++
3 files changed, 58 insertions(+), 2 deletions(-)Show Patch (Full Diff)
Include the actual code changes:
git log -p
# Or just the last commit
git log -p -1Custom Format
Create your own format with placeholders:
# Custom format
git log --pretty=format:"%h - %an, %ar : %s"
# Output:
# a1b2c3d - Sarah Johnson, 2 hours ago : Add user authentication
# f4e5d6c - Sarah Johnson, 1 day ago : Fix navigation bugUseful format placeholders:
%h- Abbreviated commit hash%H- Full commit hash%an- Author name%ae- Author email%ad- Author date%ar- Author date, relative (e.g., "2 hours ago")%s- Commit subject (first line of message)%b- Commit body
Visual Graph
Display commit history as a graph (more useful with branches):
git log --graph --oneline --all* a1b2c3d (HEAD -> main) Add user authentication
* f4e5d6c Fix navigation bug
* c7d8e9f Update homepage design
* b0a1c2d Initial commitComparing Commits
Comparing Working Directory with Last Commit
# See unstaged changes
git diff
# See staged changes
git diff --stagedComparing Specific Commits
# Compare two commits
git diff a1b2c3d f4e5d6c
# Compare HEAD with a specific commit
git diff HEAD a1b2c3d
# Compare with commits relative to HEAD
git diff HEAD HEAD~1 # Compare last two commits
git diff HEAD~3 HEAD # Compare HEAD with 3 commits agoComparing Specific Files
# See changes to a specific file
git diff filename.txt
# Compare a file between two commits
git diff a1b2c3d f4e5d6c -- filename.txtSummary of Changes
# Show only file names that changed
git diff --name-only
# Show file names with status (modified, added, deleted)
git diff --name-status
# Show statistics without full diff
git diff --stat👀 Reading Diff Output
In diff output:
- Lines starting with
-(red) were removed - Lines starting with
+(green) were added @@shows line numbers where changes occurred- Context lines (unchanged) appear without
+or-
Understanding HEAD and References
What is HEAD?
HEAD is a special pointer that refers to your current position in the Git history. Think of it as "you are here" marker.
Normally: HEAD points to the latest commit on your current branch. When you make a new commit, HEAD moves forward to point to the new commit.
Special case: HEAD can point directly to a specific commit instead of a branch (called "detached HEAD state"—we'll cover this later).
Relative References
You can refer to commits relative to HEAD:
HEAD- Current commitHEAD~1orHEAD~- One commit before HEADHEAD~2- Two commits before HEADHEAD~3- Three commits before HEAD- And so on...
You can also use caret notation:
HEAD^- Same asHEAD~1HEAD^^- Same asHEAD~2
Examples of using references:
# Show the commit before HEAD
git show HEAD~1
# Compare current state with 3 commits ago
git diff HEAD~3
# View log starting from 5 commits ago
git log HEAD~5..HEADViewing Where HEAD Points
# See what HEAD points to
cat .git/HEAD
# Output might be:
# ref: refs/heads/mainNavigating Through History
Checking Out Old Commits (Read-Only)
You can look at old versions of your project without changing anything:
# Look at a specific commit
git checkout a1b2c3dWarning: This puts you in "detached HEAD" state. You're looking at an old commit, and any changes you make won't be on a branch unless you create one.
To get back to your main branch:
git checkout mainViewing Files from Past Commits
Look at specific files without checking out the whole commit:
# View a file as it was in a specific commit
git show a1b2c3d:filename.txt
# Save an old version to a new file
git show HEAD~5:index.html > old-index.htmlSearching Through History
Find when a specific change was introduced:
# Search for when a string was added or removed
git log -S "function login"
# Show commits that changed a specific file
git log -- filename.txt
# Show commits with patches for a file
git log -p -- filename.txtInspecting Commit Metadata
See Commit Details
# Show everything about a commit
git show a1b2c3d
# Show only metadata (no diff)
git show --no-patch a1b2c3d
# Show commit in different format
git show --format=fuller a1b2c3dList Files in a Commit
# Show files changed in a commit
git show --name-only a1b2c3d
# Show files with status
git show --name-status a1b2c3d
# Show statistics
git show --stat a1b2c3dView Commit by Message
Find commits by searching their messages:
# Find commits mentioning "login"
git log --grep="login"
# Case-insensitive search
git log --grep="LOGIN" -i
# Show only matching commits with patches
git log --grep="bug" -pCreating Useful Aliases
Save time by creating shortcuts for common log commands:
# Create handy aliases
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
# Now you can use them:
git lg
git last
git histSome useful aliases to consider:
# Beautiful log with graph
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Show last commit
git config --global alias.last "log -1 HEAD"
# Show what changed in last commit
git config --global alias.dlast "diff --cached HEAD^"
# Undo last commit (keep changes)
git config --global alias.undo "reset HEAD~1 --mixed"⚡ Productivity Boost
Well-chosen aliases can save you dozens of keystrokes per day. Take time to create aliases for your most-used commands—your future self will thank you!
Practical Examples
Example 1: Finding When a Bug Was Introduced
Suppose a bug appeared recently. Find when it was introduced:
# Search for commits that changed the problematic function
git log -S "calculateTotal" -p
# Or search commit messages
git log --grep="total" -i --oneline
# View the specific commit
git show a1b2c3dExample 2: Understanding a File's Evolution
See how a specific file changed over time:
# View all commits that modified this file
git log --oneline -- app.js
# See the full history with diffs
git log -p -- app.js
# See who changed each line (we'll learn more about this later)
git blame app.jsExample 3: Compare Your Work with Last Week
# See all changes from last week
git diff HEAD@{1.week.ago} HEAD
# Or view commits from last week
git log --since="1 week ago" --oneline
# Detailed view of what changed
git log --since="1 week ago" --statExample 4: Review Before Sharing
Before pushing your work, review what you've done:
# See commits you're about to push
git log origin/main..HEAD --oneline
# See the full diff
git diff origin/main..HEAD
# Review each commit individually
git log origin/main..HEAD -pPractice Commands
Try these commands to explore commit history:
Explore Commit History
Practice viewing and inspecting commits
Try these examples:
Best Practices for Commit History
1. Make Your History Readable
Write clear commit messages that explain not just what changed, but why. Your history is documentation!
2. Commit Often
Frequent, small commits make it easier to understand changes and find when bugs were introduced.
3. Use Consistent Message Format
Many teams use conventions like:
feat: Add new feature
fix: Fix bug in authentication
docs: Update README
style: Format code
refactor: Restructure login component
test: Add tests for payment module4. Review Before Committing
Always use git diff or git diff --staged to review changes before committing.
5. Keep Related Changes Together
Each commit should represent one logical change. Don't mix unrelated changes in the same commit.
6. Don't Commit Half-Finished Work
Each commit should leave the project in a working state. If you're not done, don't commit yet (or use branches—which we'll learn next!).
Command Reference
Here's a quick reference of all the commands covered in this lesson:
# Viewing History
git log # Full commit history
git log --oneline # Compact one-line format
git log -n 5 # Show last 5 commits
git log --since="1 week ago" # Filter by date
git log --author="Name" # Filter by author
git log --grep="search" # Search commit messages
git log --stat # Include file statistics
git log -p # Include full diffs
git log --graph --oneline --all # Visual graph
# Viewing Specific Commits
git show # Show latest commit
git show a1b2c3d # Show specific commit
git show HEAD~1 # Show previous commit
git show --stat # Show with statistics
# Comparing Versions
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD~1 # Compare with previous commit
git diff a1b2c3d b2c3d4e # Compare two commits
git diff --name-only # Show only changed files
# Navigation
git checkout a1b2c3d # View old commit (detached HEAD)
git checkout main # Return to branch
# Searching
git log -S "text" # Find when text was added/removed
git log -- filename.txt # Commits affecting a file
git log --grep="pattern" # Search commit messagesKey Takeaways
- Each commit is a complete snapshot with a unique SHA-1 hash
git loghas many options for viewing and filtering historygit showdisplays detailed information about specific commitsgit diffcompares different versions of your code- HEAD is a pointer to your current position in the history
- Use relative references like
HEAD~1to refer to previous commits - Commit hashes uniquely identify every commit in your repository
- You can search history by message, author, date, or content
- Git stores snapshots, not differences, making operations fast
- Good commit practices make your history a valuable documentation tool
What's Next?
Excellent work! You now have a deep understanding of commits and how to navigate your project's history. You can view detailed information about any commit, compare different versions, and search through your project's timeline.
In the next lesson, we'll explore one of Git's most powerful features: branches. Branches let you work on different features simultaneously, experiment without risk, and organize your development workflow. You'll learn how to create branches, switch between them, and understand why branching is essential for modern software development.
🎯 Practice Assignment
Before moving on, practice exploring your repository history:
- Create at least 5 more commits in your repository
- Use
git logwith different options to view them - Use
git showto inspect specific commits - Use
git diffto compare different versions - Try searching your history with
--grepand-S - Create at least one useful alias for yourself