Back to Git & Dev Tools
2026-02-145 min read

advanced git log tutorial (Git & Dev Tools)

Learn advanced git log tutorial (Git & Dev Tools) step by step with clear examples and exercises.

Title: Advanced Git Log Tutorial (Git & Dev Tools)

Why This Matters

In software development, version control systems are essential for managing and tracking changes to codebases. Git, a distributed version control system, is widely used due to its efficiency, flexibility, and ease of use. The advanced git log command is an indispensable tool for developers, providing insights into the project's history, identifying issues, and facilitating collaboration.

By understanding and mastering the advanced git log command, developers can efficiently navigate complex commit histories, troubleshoot issues, and visualize the evolution of their projects over time.

Prerequisites

Before diving into the advanced git log tutorial, it is essential to have a basic understanding of Git and its fundamental commands:

  1. Initializing a repository (git init)
  2. Adding files to the staging area (git add)
  3. Committing changes (git commit)
  4. Viewing commit history (git log)
  5. Creating branches (git branch)
  6. Switching between branches (git checkout)
  7. Merging branches (git merge)
  8. Understanding basic Git commands like git status, git diff, and git blame
  9. Familiarity with common Git workflows, such as feature branches and pull requests
  10. Basic understanding of regular expressions for customizing the output format

Core Concept

The git log command displays the commit history of a repository, providing information such as author, date, and commit message for each change. However, Git's advanced log format offers more detailed insights into the project's evolution.

To access this enhanced log format, use the --pretty=format: option followed by a custom format string. The format string can be tailored to display specific information in a preferred order and format.

Here's an example of using the advanced git log command with a custom format:

git log --pretty=format:'%h - %an, %ar (%cr): %s' --graph --date=short

In this example, the format string consists of placeholders that represent different pieces of information:

  • %h: The abbreviated commit hash
  • %an: The author's name
  • %ar: The author date (relative)
  • %cr: The commit date (absolute)
  • %s: The commit message subject
  • --graph: Displays a graphical representation of the commit history
  • --date=short: Shows the commit dates in a short format (YYYY-MM-DD)

Customizing the Output Format

The custom format string can be further extended to include additional information, such as file paths, lines added or removed, and more. For example, you can use regular expressions to match specific patterns within commit messages.

git log --pretty=format:'%h - %an, %ar (%cr): %s - %b' --graph --date=short

In this example, the %b placeholder represents the full commit message, including the body and footer.

Worked Example

Let's walk through an example using the advanced Git log command with a custom format. Suppose we have a simple repository containing multiple files, and we will create, modify, and commit changes to these files to demonstrate the advanced Git log.

  1. Initialize the repository:
git init
  1. Create and initialize some files:
touch file1.txt file2.txt file3.txt
git add .
git commit -m "Initial commit"
  1. Modify the files and create new commits with the advanced Git log command:
echo "Modified content 1" >> file1.txt
git add file1.txt
git commit -m "Modify file1.txt" --amend --no-edit

echo "Added new content to file2.txt" >> file2.txt
git add file2.txt
git commit -m "Add content to file2.txt"

echo "Modified and deleted lines in file3.txt" > file3.txt
git add file3.txt
git commit -m "Modify and delete lines in file3.txt" --amend --no-edit
  1. View the commit history with the advanced Git log command:
git log --pretty=format:'%h - %an, %ar (%cr): %s' --graph --date=short

Output:

* 38d9f - Your Name, 1 minute ago (2 days ago): Modify and delete lines in file3.txt
* 7c6b5 - Your Name, 2 minutes ago (2 days ago): Add content to file2.txt
| * 42f6d - Your Name, 3 minutes ago (2 days ago): Modify file1.txt
|/
* 42f6d - Your Name, 4 minutes ago (2 days ago): Initial commit

Customizing the Output for Specific Commits

You can also customize the output format for specific commits by appending the commit hash to the command:

git log 42f6d^..HEAD --pretty=format:'%h - %an, %ar (%cr): %s' --graph --date=short

This command displays only the commits between the parent of the initial commit (42f6d) and the current HEAD.

Common Mistakes

  1. Forgetting to specify the custom format with --pretty:
git log --format:'%h - %an, %ar: %s'
  1. Using single quotes instead of double quotes around the format string:
git log --pretty='%h - %an, %ar: %s'
  1. Not escaping special characters within the format string:
git log --pretty=format:'%h - %an, %ar: %s'
  1. Not using --amend when modifying the most recent commit to avoid creating a new one:
git commit -m "Modify file1.txt"
git commit -m "Add content to file2.txt" (instead of --amend)

Common Mistakes - Practice Questions

  1. Modifying the most recent commit without using --amend:
git commit -m "Modify file1.txt"
git commit -m "Add content to file2.txt"
  1. Using a single quote instead of a double quote around the format string:
git log --pretty='%h - %an, %ar: %s'
  1. Forgetting to escape special characters within the format string:
git log --pretty=format:'%h - %an, %ar: %s'

Practice Questions

  1. Write a custom Git log command to display the author name, commit date (absolute), and commit message for the last 5 commits, with the graphical representation of the commit history:
git log --pretty=format:'%h - %an, %ad (%cr): %s' --graph --date=short --max-count=5
  1. Create a repository with multiple files, modify them, and use the advanced Git log command to view the history in reverse chronological order, including the file path and the number of lines added or removed:
git log --pretty=format:'%h - %an, %ar (%cr): %s - %b' --graph --date=short
  1. Write a script that generates a report containing the total number of commits per author for a given Git repository:
#!/bin/sh
authors=$(git shortlog -sn)
echo "Author\tCommits"
while read author commits; do
echo "$author\t$commits"
done <<< "$authors"

FAQ

  1. What is the difference between git log and git reflog?
  • git log displays the commit history of the current branch, while git reflog shows the complete history of all refs (branches, tags, etc.) in the repository.
  1. Can I customize the output of git log for a specific commit?
  • Yes! Use the --pretty=format: option with the commit hash: git log --pretty=format:'%h - %an, %ar (%cr): %s'.
  1. What does the --amend option do in a Git commit?
  • The --amend option allows you to modify the most recent commit by adding or changing files and updating the commit message without creating a new commit.
  1. How can I view the changes made in a specific file across multiple commits using the advanced Git log command?
  • Use the git log --patch command to display the differences between each commit for a specific file:
git log --pretty=format:'%h - %an, %ar (%cr): %s' --graph --date=short -- <file>
advanced git log tutorial (Git & Dev Tools) | Git & Dev Tools | XQA Learn