How to view Git commit log?

GitGitBeginner
Practice Now

Introduction

Understanding how to view and navigate Git commit logs is crucial for developers tracking project evolution and code changes. This tutorial will explore various methods to effectively examine commit history, helping you gain deeper insights into your Git repository's development timeline.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/log("`Show Commits`") git/BranchManagementGroup -.-> git/shortlog("`Condensed Logs`") git/BranchManagementGroup -.-> git/reflog("`Log Ref Changes`") subgraph Lab Skills git/log -.-> lab-418156{{"`How to view Git commit log?`"}} git/shortlog -.-> lab-418156{{"`How to view Git commit log?`"}} git/reflog -.-> lab-418156{{"`How to view Git commit log?`"}} end

Git Commit Log Basics

What is a Git Commit Log?

A Git commit log is a comprehensive record of all changes made to a repository, tracking the project's history and evolution. Each commit represents a specific snapshot of the project at a particular point in time, capturing the modifications, additions, and deletions made by developers.

Key Components of a Commit Log

Component Description
Commit Hash Unique identifier for each commit
Author Person who made the changes
Date Timestamp of the commit
Commit Message Description of the changes

Understanding Commit History

gitGraph commit commit branch develop checkout develop commit commit checkout main merge develop commit

Basic Git Log Commands

  1. Basic Log View
## View commit history
git log
  1. Simplified Log View
## Compact log view
git log --oneline

Why Commit Logs Matter

Commit logs are crucial for:

  • Tracking project development
  • Understanding code changes
  • Collaboration and code review
  • Debugging and rollback

Best Practices for Commit Logging

  • Write clear, concise commit messages
  • Use imperative mood in messages
  • Explain the "why" behind changes
  • Keep commits focused and atomic

At LabEx, we emphasize the importance of maintaining clean and informative commit logs to enhance project transparency and collaboration.

Viewing Commit History

Basic Log Viewing Methods

Standard Log View

## Display full commit history
git log

Compact Log View

## Show abbreviated commit information
git log --oneline

Advanced Log Viewing Techniques

Displaying Specific Number of Commits

## Show last 5 commits
git log -n 5

Detailed Commit Information

## Show comprehensive commit details
git log --stat

Graphical Log Representation

gitGraph commit commit branch feature checkout feature commit commit checkout main merge feature commit

Log Formatting Options

Option Description
--pretty=format:"%h %an %s" Custom log format
--graph Show branch graph
--decorate Show branch references

Viewing Commits for Specific Paths

## Log for specific file or directory
git log -- path/to/file

Date-Based Log Filtering

## Commits within a date range
git log --since="2023-01-01" --until="2023-12-31"

Comparing Branches

## Log comparing two branches
git log main..feature-branch

At LabEx, we recommend mastering these log viewing techniques to enhance your Git workflow and project understanding.

Filtering Log Results

Basic Filtering Techniques

Filtering by Author

## Show commits by specific author
git log --author="John Doe"

Filtering by Commit Message

## Find commits with specific keyword
git log --grep="bugfix"

Advanced Filtering Methods

Date-Based Filtering

## Commits within specific date range
git log --since="2 weeks ago" --until="yesterday"

Commit Range Filtering

## Commits between two references
git log main..feature-branch

Filtering by File Changes

Commits Affecting Specific File

## Log for specific file
git log -- path/to/file.txt

Commits with File Modifications

## Commits with specific file changes
git log --name-status

Complex Filtering Combinations

## Multiple filter conditions
git log --author="Jane" --since="2023-01-01" --grep="feature"

Filtering Options Overview

Filter Option Description
--author Filter by commit author
--grep Search commit messages
--since/--until Date-based filtering
--name-status Show file changes

Visualization of Commit Filtering

flowchart LR A[All Commits] --> B{Filter Conditions} B -->|Author| C[Filtered by Author] B -->|Date| D[Filtered by Date] B -->|Message| E[Filtered by Message]

Practical Filtering Scenarios

Recent Team Contributions

## Last week's team commits
git log --since="1 week ago" --author="[email protected]"

Tracking Specific Changes

## Commits related to specific feature
git log --grep="authentication" --name-status

At LabEx, we emphasize the power of precise log filtering to enhance code review and project tracking.

Summary

Mastering Git commit log techniques empowers developers to efficiently track project progress, analyze code changes, and understand the collaborative journey of software development. By leveraging these log viewing strategies, you can enhance your version control skills and maintain a comprehensive overview of your Git repository.

Other Git Tutorials you may like