Viewing the commit history in Git is essential for understanding the changes made to a repository over time. Git provides several commands to inspect the commit history, including git log, git show, and git reflog. Below are the steps to view the commit history, along with sample commands and explanations.

Viewing the Commit History with git log

The git log command is the most commonly used command to view the commit history. It displays a list of commits in reverse chronological order, showing details such as the commit hash, author, date, and commit message.

    
# View the commit history
git log

# Output example:
# commit abc123 (HEAD -> main)
# Author: John Doe <john@example.com>
# Date: Mon Oct 2 12:00:00 2023 +0000
#
# Added new features and fixed bugs
#
# commit def456
# Author: Jane Smith <jane@example.com>
# Date: Sun Oct 1 10:00:00 2023 +0000
#
# Initial commit

Customizing the git log Output

You can customize the output of git log using various options. Here are some commonly used options:

  • --oneline: Displays each commit on a single line.
  • --graph: Displays a text-based graph of the commit history.
  • --stat: Shows statistics about the changes in each commit.
  • --author: Filters commits by a specific author.
  • --since: Shows commits made after a specific date.
    
# View the commit history in a single line per commit
git log --oneline

# Output example:
# abc123 (HEAD -> main) Added new features and fixed bugs
# def456 Initial commit

# View the commit history with a graph
git log --graph

# Output example:
# * abc123 (HEAD -> main) Added new features and fixed bugs
# * def456 Initial commit

# View the commit history with statistics
git log --stat

# Output example:
# commit abc123 (HEAD -> main)
# Author: John Doe <john@example.com>
# Date: Mon Oct 2 12:00:00 2023 +0000
#
# Added new features and fixed bugs
#
# file1.txt | 5 +++--
# file2.txt | 3 ++-
# 2 files changed, 5 insertions(+), 3 deletions(-)

Viewing a Specific Commit with git show

The git show command allows you to view the details of a specific commit, including the changes made in that commit.

    
# View the details of a specific commit
git show abc123

# Output example:
# commit abc123 (HEAD -> main)
# Author: John Doe <john@example.com>
# Date: Mon Oct 2 12:00:00 2023 +0000
#
# Added new features and fixed bugs
#
# diff --git a/file1.txt b/file1.txt
# index 1234567..89abcdef 100644
# --- a/file1.txt
# +++ b/file1.txt
# @@ -1,3 +1,5 @@
# +New feature
# Existing content
# -Old feature

Viewing the Reference Log with git reflog

The git reflog command shows a log of all reference updates, including branch switches and commits. This is useful for recovering lost commits or branches.

    
# View the reference log
git reflog

# Output example:
# abc123 (HEAD -> main) HEAD@{0}: commit: Added new features and fixed bugs
# def456 HEAD@{1}: checkout # Initial commit
# ghi789 HEAD@{2}: commit: Updated project documentation

Conclusion

Viewing the commit history in Git is crucial for tracking changes and understanding the evolution of a project. By using commands like git log, git show, and git reflog, you can gain insights into the development process, review changes, and recover lost commits if necessary. Mastering these commands will enhance your ability to manage and collaborate on projects effectively.