Falytom logoFALYTOM
Games
ToolsUI ComponentsServicesProductsAbout
menu
  1. Home
  2. /Web Development
  3. /Github
  4. /Commits And History
Your Progress0%
0 of 20 completed

Git & Github Topics

Getting Started

  • What is Git?
  • What is GitHub?
  • Installing & Configuring Git
  • Your First Git Repository

Core Git Concepts

  • Understanding Commits & History
  • Working with Branches
  • Merging Branches
  • The Staging Area
  • Undoing Changes

Working with GitHub

  • Connecting to GitHub
  • Pushing & Pulling Code
  • Cloning Repositories
  • GitHub README & Documentation

Collaboration Workflows

  • Forking & Pull Requests
  • Code Review Basics
  • Handling Merge Conflicts
  • Issues & Project Management

Best Practices & Workflows

  • Git Workflow Best Practices
  • Working with .gitignore
  • Common Git Problems & Solutions

Understanding Commits & History

Deep dive into commits, viewing history, and navigating your project timeline

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:

TEXT
a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0

Key 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

BASH
git log

This shows the full commit history with all details:

TEXT
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 colors

Compact One-Line Format

For a cleaner overview, use --oneline:

BASH
git log --oneline
TEXT
a1b2c3d (HEAD -> main) Add user authentication feature
f4e5d6c Fix navigation menu overflow on mobile devices
c7d8e9f Update homepage design with new brand colors
b0a1c2d Initial commit

This shows just the abbreviated hash and the first line of the commit message.

Limiting Output

Show only the last few commits:

BASH
# Show last 5 commits
git log -5

# Or use -n
git log -n 3

# Combine with --oneline
git log --oneline -5

Filtering by Date

BASH
# 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

BASH
# Commits by a specific author
git log --author="Sarah"

# Using full email
git log --author="sarah@example.com"

Searching Commit Messages

BASH
# 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:

BASH
# 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 HEAD

The 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:

BASH
# 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.html

Show with Statistics

BASH
# Show commit with file change statistics
git show --stat

# Show commit with compact stat
git show --shortstat

Advanced Log Formatting

Include File Statistics

See which files changed and how much:

BASH
git log --stat

Output shows files changed with addition/deletion counts:

TEXT
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:

BASH
git log -p

# Or just the last commit
git log -p -1

Custom Format

Create your own format with placeholders:

BASH
# 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 bug

Useful 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):

BASH
git log --graph --oneline --all
TEXT
* a1b2c3d (HEAD -> main) Add user authentication
* f4e5d6c Fix navigation bug
* c7d8e9f Update homepage design
* b0a1c2d Initial commit

Comparing Commits

Comparing Working Directory with Last Commit

BASH
# See unstaged changes
git diff

# See staged changes
git diff --staged

Comparing Specific Commits

BASH
# 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 ago

Comparing Specific Files

BASH
# See changes to a specific file
git diff filename.txt

# Compare a file between two commits
git diff a1b2c3d f4e5d6c -- filename.txt

Summary of Changes

BASH
# 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 commit
  • HEAD~1 or HEAD~ - One commit before HEAD
  • HEAD~2 - Two commits before HEAD
  • HEAD~3 - Three commits before HEAD
  • And so on...

You can also use caret notation:

  • HEAD^ - Same as HEAD~1
  • HEAD^^ - Same as HEAD~2

Examples of using references:

BASH
# 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..HEAD

Viewing Where HEAD Points

BASH
# See what HEAD points to
cat .git/HEAD

# Output might be:
# ref: refs/heads/main

Navigating Through History

Checking Out Old Commits (Read-Only)

You can look at old versions of your project without changing anything:

BASH
# Look at a specific commit
git checkout a1b2c3d

Warning: 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:

BASH
git checkout main

Viewing Files from Past Commits

Look at specific files without checking out the whole commit:

BASH
# 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.html

Searching Through History

Find when a specific change was introduced:

BASH
# 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.txt

Inspecting Commit Metadata

See Commit Details

BASH
# 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 a1b2c3d

List Files in a Commit

BASH
# 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 a1b2c3d

View Commit by Message

Find commits by searching their messages:

BASH
# 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" -p

Creating Useful Aliases

Save time by creating shortcuts for common log commands:

BASH
# 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 hist

Some useful aliases to consider:

BASH
# 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:

BASH
# 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 a1b2c3d

Example 2: Understanding a File's Evolution

See how a specific file changed over time:

BASH
# 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.js

Example 3: Compare Your Work with Last Week

BASH
# 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" --stat

Example 4: Review Before Sharing

Before pushing your work, review what you've done:

BASH
# 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 -p

Practice 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:

TEXT
feat: Add new feature
fix: Fix bug in authentication
docs: Update README
style: Format code
refactor: Restructure login component
test: Add tests for payment module

4. 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:

BASH
# 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 messages

Key Takeaways

  • Each commit is a complete snapshot with a unique SHA-1 hash
  • git log has many options for viewing and filtering history
  • git show displays detailed information about specific commits
  • git diff compares different versions of your code
  • HEAD is a pointer to your current position in the history
  • Use relative references like HEAD~1 to 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:

  1. Create at least 5 more commits in your repository
  2. Use git log with different options to view them
  3. Use git show to inspect specific commits
  4. Use git diff to compare different versions
  5. Try searching your history with --grep and -S
  6. Create at least one useful alias for yourself

Test Your Understanding of Commits & History

Question 1 of 4

What is a commit hash?

Current Score0 / 0

Know someone who'd find this guide helpful? Please share!

Previous
Your First Git Repository
Next
Working with Branches

Never Miss a New Git & GitHub Tutorial

Join 2,000+ developers learning Git & GitHub step-by-step. Get new tutorials, tips, and exclusive resources delivered to your inbox - completely FREE.

You might also like

UI Components

Ready-made components for your projects

Developer Tools

Boost productivity with handy generators

HTML Game

Learn HTML concepts through interactive gameplay

Join Our Web Development Training Program

Master frontend & backend development with hands-on projects.

Git & Github Tutorials

0 of 20 completed

Your Progress0%

Topics

Getting Started

  • What is Git?
  • What is GitHub?
  • Installing & Configuring Git
  • Your First Git Repository

Core Git Concepts

  • Understanding Commits & History
  • Working with Branches
  • Merging Branches
  • The Staging Area
  • Undoing Changes

Working with GitHub

  • Connecting to GitHub
  • Pushing & Pulling Code
  • Cloning Repositories
  • GitHub README & Documentation

Collaboration Workflows

  • Forking & Pull Requests
  • Code Review Basics
  • Handling Merge Conflicts
  • Issues & Project Management

Best Practices & Workflows

  • Git Workflow Best Practices
  • Working with .gitignore
  • Common Git Problems & Solutions
Falytom logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and more through interactive games, tutorials, components and free tools. Transform your businesses with AI chatbots, SEO-optimized websites, and professional development services.

© 2025 Tomilola Group
  • Facebook logo
  • Twitter logo
  • Instagram logo
  • LinkedIn logo
  • GitHub logo
  • YouTube logo