Skip to main content

Command Palette

Search for a command to run...

How to view the commit history in Git

Updated
4 min read
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:

  1. 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, quit git log with q.
  2. To display each commit as a single line with its short SHA-1 hash and commit message, run:

     git log --oneline
    

  3. To show more details about all previous commits, run:

     git log -p
    

    💡
    To limit output to the last two commits, you can add the -2 option: git log -p -2
  4. To show how many files changed and the number of insertions (+) and deletions (-), run:

     git log --stat
    

  5. 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.

  6. 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.

  1. 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).

      💡
      If --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.
  2. To show commit stats with the pretty option, run:

     git log --pretty=format:"%Cred %h - %Cgreen %an: %C(yellow) %s" --stat
    

  3. 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>"'
    

  4. 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.

  1. To show all commits done by 2 people, we can use regex:

     git log --author="John Doe\|Alice"
    

  2. 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

  3. 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".

  4. 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.

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

     git log -5
    

  6. To list only merge commits, run:

     git log --merges
     git log --no-merges
    
    • --no-merges - excludes merge commits from the output, run:

  1. 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.

  2. To get a precise result, we can combine multiple filters:

     git log --author="John Doe" --grep="bug-fix" --since="10 weeks ago"
    

References

  1. git log documentation