Horje
Customizing Git Log Output for Clearer History

Git is a powerful version control system that allows developers to manage changes to their codebase efficiently. One of Git’s most useful features is the git log command, which displays the commit history of a repository. However, the default output of git log can sometimes be overwhelming and difficult to interpret, especially in large projects. Customizing the Git log output can make the history clearer and more useful. This article will guide you through various customization options to achieve a clearer and more informative Git history.

Basic Git Log Usage

The basic git log command shows a list of commits in the current branch. Each entry includes the commit hash, author, date, and commit message:

git log

While this output is informative, it can be difficult to scan quickly. Fortunately, Git provides several options to customize this output.

Why Customize Git Log Output ?

The default output of git log provides essential information about commits, such as commit hash, author, date, and commit message. As projects become larger, the default output can become too detailed and lose its focus. Customizing the Git log output helps in:

  • Improved Readability: Tailoring the output to display only relevant information improves readability, making it easier to scan through commit history.
  • Focused Insights: Highlighting specific details like commit messages, file changes, or authorship can provide focused insights into project developments.
  • Visual Clarity: Utilizing formatting options and visual cues enhances the clarity of commit history, aiding in understanding branching, merges, and individual commits.

Customization Options

Formatting Commit Output

You can format the output of git log using placeholders and formatting options to display relevant details. Here are some commonly used placeholders:

  • %H: Commit hash
  • %an: Author name
  • %ae: Author email
  • %ad: Author date
  • %s: Commit subject (first line of the commit message)
  • %n: Newline

Limiting Output

Limiting the number of commits displayed can prevent information overload, especially in large repositories.

git log -5  // Displays the last 5 commits

Filtering Commits

Filtering commits based on criteria such as author, date range, or specific paths can provide a focused view of relevant changes.

git log --author="John Doe" --since="2022-01-01" -- path/to/file

Graphical Representation

Adding a graphical representation (ASCII art) of commit history using --graph and --oneline options can visually illustrate branching and merging.

git log --oneline --graph

Accessing Git Log

Step 1: Open a terminal or command prompt. Navigate to the Git repository directory using the cd command.

cd Name_of_Repository 

Step 2: Then type git log and press enter to view the default log output. 

git log

The default log displays commits in reverse chronological order, showing commit hashes, authors, dates, and commit messages.

Screenshot-2024-03-02-084353

git log

Customizing Log Output

We can customize the git log output by using different formatting options. Common formatting options include –oneline, –graph, –decorate, –stat, and –pretty.

  • –oneline: Displays each commit on a single line, showing the commit hash and the first line of the commit message. 
  • –graph: Draws a text-based graph of the commit history, showing branching and merging visually.
  • –decorate: Adds additional information to commits, such as branch and tag names, making it easier to understand which commits belong to which branches or tags.
  • –stat: Includes statistics about file changes (insertions and deletions) in each commit, providing insights into the project’s evolution.
  • –pretty: Allows customizing the output format using placeholders like %H (commit hash), %an (author name), %s (subject), %cd (commit date), etc. This option offers extensive flexibility in defining the log output structure.

–oneline

It can be used to display a concise, one-line summary of each commit. For example – 

git log --oneline
Screenshot-2024-03-02-084410

Using –oneline option

–graph

Draws a text picture of the project’s history, showing branches and merges. For example – 

git log --graph
Screenshot-2024-03-02-084428

Using –graph option

–decorate

Shows the names of branches and tags next to each commit. For example – 

 git log --decorate
Screenshot-2024-03-02-084453

Using –decorate option

–stat

It can be used to get the number of insertions and deletions made in each commit. For example – 

 git log --stat
Screenshot-2024-03-02-084619

Using –stat option

–pretty=format:”…”

It lets you create a custom format for showing commits. This is the most customizable option. For example, the command below will display commits in a format like abbreviated-hash – author-name, date : commit-message.

 git log --pretty=format:"%h - %an, %ar : %s"
Screenshot-2024-03-02-084730

Using –pretty option




Reffered: https://www.geeksforgeeks.org


Git

Related
Git LFS: Managing Large Files in Git Repositories Git LFS: Managing Large Files in Git Repositories
Managing Git Repositories with GitLab Managing Git Repositories with GitLab
Using Git Attributes for Keyword Expansion and Substitution Using Git Attributes for Keyword Expansion and Substitution
Git Workflows For Agile Development Teams Git Workflows For Agile Development Teams
Git Workflows With Open Source Collaboration Git Workflows With Open Source Collaboration

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13