How to view the commit history in Git

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
What is a GPG key? GnuPG is a tool for secure communication and uses public-key cryptography. In a public-key system, each user has a pair of keys consisting of a private key and a public key. A user's private key is kept secret; it need never be rev...
Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log:
To show commit history in reverse chronological order, run:
git log

git log with q.To display each commit as a single line with its short SHA-1 hash and commit message, run:
git log --oneline

To show more details about all previous commits, run:
git log -p

-2 option: git log -p -2To show how many files changed and the number of insertions (+) and deletions (-), run:
git log --stat

To group commits by author and display commit messages under each author's name, run:
git shortlog

This command can be used to quickly see who contributed the most to a repository, to get an overview of contributions instead of a detailed history, and to prepare contributor statistics for reports or release notes.
To visualize some work with branches, run the command:
git log --graph --oneline
--graph → Displays a commit graph showing branch relationships.
--oneline → Shows each commit in a single line.

We can see that second-branch was created and merged with the master branch.
To completely change the log output to display a structured and visually enhanced commit history, use the --pretty option:
git log --pretty="%C(yellow) Commit: %h %C(magenta)Date: %ad %C(red) Message: %s %C(cyan) Author: %an" --date=human
--pretty="..." → Customizes the format of the log output.
%C(yellow) → Changes Hash: text color to yellow.
%h → Displays the short commit hash.
%ad → Displays a date of commit.
%s → Shows the commit message.
%an → Shows who committed the changes.
--date=human → Formats the date in a human-readable way (e.g., "2 days ago" instead of a timestamp).

--pretty="" contains a custom format string (using %h, %s, %C for colors, etc.), Git treats it as format: automatically. Therefore, you don’t need to explicitly write format: when using a custom format.To show commit stats with the pretty option, run:
git log --pretty=format:"%Cred %h - %Cgreen %an: %C(yellow) %s" --stat

To create an alias called lg , we need to change the configuration file (config), on global table (--global) by adding the following entry:
git config --global alias.lg 'log --color --graph --pretty="%C(red) %h%Creset - %C(yellow) %s %C(green) (%cr) %C(blue) <%an>"'

To check the alias definition, run the command:
git config --global --get alias.lg

git logSo far we used ALL information from git log. It is possible to narrow the search by querying though the git log. We can query through git log using various filters and options to find specific commits based on author, date, message, file changes, branches, and more.
To show all commits done by 2 people, we can use regex:
git log --author="John Doe\|Alice"

To search for commit by date, run:
git log --since="2024-01-01" --until="2024-02-01"
This will show commits made between Jan 1, 2024, and Feb 1, 2024
To search for commits by message (keyword search) and make the search case-insensitive, run:
git log --grep="Feature" --grep="bug fix" --regexp-ignore-case
This will find commits where the message contains both "bug fix", ‘'“BUG FIX“, “Bug fix“, “FEATURE“, “Feature“, "feature".

To search for commits that modified several files, run:
git log -- testfile-01 branchfile-01
This will show commits affecting testfile-01 and branchfile-01.

-- we inform git that we have files in mind, not branches.To show the last 5 commits, run:
git log -5

To list only merge commits, run:
git log --merges
git log --no-merges
--no-merges - excludes merge commits from the output, run: 
To search for commits from a specific branch, run:
git log main
git log master..second-branch
git log second-branch..master

git log master..second-branch compares the commit history of both branches and shows commits that exist in second-branch but are missing in master. Once the branch second-branch is merged into master, all its commits become part of master’s history. Therefore, Git finds nothing unique in second-branch and the output returns nothing
git log second-branch..master shows commits in master that second-branch doesn’t have.
To get a precise result, we can combine multiple filters:
git log --author="John Doe" --grep="bug-fix" --since="10 weeks ago"