git log Command


git log Command

The git log command is used to display the commit history of the current branch in a Git repository. It shows a list of commits, including information such as commit hashes, author names, dates, and commit messages. This helps you review the changes made to the repository over time.


What Does git log Show?

When you run git log, Git provides detailed information about each commit:

  1. Commit Hash: A unique identifier (hash) for each commit, usually a 40-character SHA-1 hash.
  2. Author Information: The name and email address of the person who made the commit.
  3. Date: The date and time when the commit was made.
  4. Commit Message: The message provided when the commit was created, describing the changes made.

Basic Syntax

git log

This command displays the commit history for the current branch, starting with the most recent commit.


Example Output of git log

Here's an example of what the output might look like

commit 9f1c1b0aaf8e69e6e1f8a0b6e6c5e8b1a22b6c1b Author: Jane Doe <jane.doe@example.com> Date: Fri Sep 6 14:23:45 2024 -0400 Fix issue with user authentication commit 7f4a6d3b7e9a76cba5f6b0b6a9a1f9a3b6e7f0b6 Author: John Smith <john.smith@example.com> Date: Thu Sep 5 10:11:23 2024 -0400 Add new feature for password reset commit 4d3a2b1c6e8f7d9a5b6f7a8b9c0e1f2d3e4b5c6d Author: Alice Johnson <alice.johnson@example.com> Date: Wed Sep 4 09:34:12 2024 -0400 Update README with installation instructions

Explanation of Each Section:

  1. Commit Hash:

    commit 9f1c1b0aaf8e69e6e1f8a0b6e6c5e8b1a22b6c1b
    • A unique identifier for the commit.
  2. Author Information:

    Author: Jane Doe <jane.doe@example.com>
    • Name and email of the person who made the commit.
  3. Date:

    Date: Fri Sep 6 14:23:45 2024 -0400
    • When the commit was made.
  4. Commit Message:

    Fix issue with user authentication
    • A description of what was changed in the commit.

Common Options for git log

  1. Show Commit History for a Specific File:

    git log <file>
    • Displays commit history for changes made to a specific file.
  2. Limit the Number of Commits:

    git log -n <number>
    • Shows the most recent <number> commits.

    Example:

    git log -n 5
  3. Show Commits with a Graph:

    git log --graph
    • Displays a visual representation of the commit history, showing the branching and merging structure.
  4. Show Commits in a One-Line Format:

    git log --oneline
    • Condenses each commit to a single line with the commit hash and message.

    Example:

    git log --oneline
  5. Show Commit History with Dates and Authors:


    git log --pretty=format:"%h - %an, %ar : %s"
    • Customizes the output to show commit hash, author name, relative date, and commit message.

    Example:

    git log --pretty=format:"%h - %an, %ar : %s"
  6. Show Commit History for a Specific Branch:

    git log <branch_name>
    • Displays commits for a specific branch.

    Example:

    git log feature-branch
  7. Show Commits Between Two Commits:

    git log <commit1>..<commit2>
    • Shows commits that are reachable from <commit2> but not from <commit1>.

    Example:

    git log HEAD~5..HEAD

Summary

  • Purpose: git log displays the commit history for the current branch, showing details about each commit.
  • Basic Syntax: git log
  • Options:
    • -n <number>: Limit the number of commits displayed.
    • --graph: Show commit history as a graph.
    • --oneline: Display each commit in a single line.
    • --pretty=format:"<format>": Customize the commit log format.
    • --since and --until: Show commits within a specific time range.

Using git log is essential for reviewing project history, understanding changes, and tracking the evolution of your codebase. It provides insights into who made changes, when they were made, and the nature of those changes.