How to view the commit history in Git

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
💡If the logs are longer than your screen, it always goes into interactive mode. To escape and come back to the shell, quitgit logwithq.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
💡To limit output to the last two commits, you can add the-2option: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
--prettyoption: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)→ ChangesHash: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).
💡If--pretty=""contains a custom format string (using%h,%s,%Cfor colors, etc.), Git treats it asformat:automatically. Therefore, you don’t need to explicitly writeformat:when using a custom format.
To show commit stats with the
prettyoption, 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), onglobaltable (--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
Querying through git log
So 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-caseThis 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-01This will show commits affecting testfile-01 and branchfile-01.
💡Using the--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-branchcompares the commit history of both branches and shows commits that exist insecond-branchbut are missing inmaster. Once the branchsecond-branchis merged intomaster, all its commits become part ofmaster’s history. Therefore, Git finds nothing unique insecond-branchand the output returns nothinggit log second-branch..mastershows commits inmasterthatsecond-branchdoesn’t have.To get a precise result, we can combine multiple filters:
git log --author="John Doe" --grep="bug-fix" --since="10 weeks ago"



