# 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:
    
    ```bash
    git log
    ```
    
    ![](https://i.imgur.com/OgsnOAH.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">If the logs are longer than your screen, it always goes into interactive mode. To escape and come back to the shell,<strong> quit </strong><code>git log</code> with <code>q</code>.</div>
    </div>
    
2. To display each commit as a single line with its short SHA-1 hash and commit message, run:
    
    ```bash
    git log --oneline
    ```
    
    ![](https://i.imgur.com/ZQEPyXS.png align="center")
    
3. To show more details about all previous commits, run:
    
    ```bash
    git log -p
    ```
    
    ![](https://i.imgur.com/5E0ZEDJ.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">To limit output to the last two commits, you can add the <code>-2</code> option: <code>git log -p -2</code></div>
    </div>
    
4. To show how many files changed and the number of insertions (+) and deletions (-), run:
    
    ```bash
    git log --stat
    ```
    
    ![](https://i.imgur.com/gMCN6gk.png align="center")
    
5. To group commits by author and display commit messages under each author's name, run:
    
    ```bash
    git shortlog
    ```
    
    ![](https://i.imgur.com/2nQUgYe.png align="center")
    
    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:
    
    ```bash
    git log --graph --oneline
    ```
    
    * `--graph` → Displays a commit graph showing branch relationships.
        
    * `--oneline` → Shows each commit in a single line.
        
    
    ![](https://i.imgur.com/CqMy9Vd.png align="center")
    
    We can see that `second-branch` was created and merged with the `master` branch.
    
7. To completely change the log output to display a structured and visually enhanced commit history, use the `--pretty` option:
    
    ```bash
    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).
        
        ![](https://i.imgur.com/npcjabs.png align="center")
        
        <div data-node-type="callout">
        <div data-node-type="callout-emoji">💡</div>
        <div data-node-type="callout-text">If <code>--pretty=""</code> contains a <strong>custom format string</strong> (using <code>%h</code>, <code>%s</code>, <code>%C</code> for colors, etc.), Git treats it as <code>format:</code> automatically. Therefore, you <strong>don’t need to explicitly write </strong><code>format:</code> when using a custom format.</div>
        </div>
        
8. To show commit stats with the `pretty` option, run:
    
    ```bash
    git log --pretty=format:"%Cred %h - %Cgreen %an: %C(yellow) %s" --stat
    ```
    
    ![](https://i.imgur.com/n8X2sFb.png align="center")
    
9. To create an alias called `lg` , we need to change the configuration file (`config`), on `global` table (`--global`) by adding the following entry:
    
    ```bash
    git config --global alias.lg 'log --color --graph --pretty="%C(red) %h%Creset - %C(yellow) %s %C(green) (%cr) %C(blue) <%an>"'
    ```
    
    ![](https://i.imgur.com/oyLAg57.png align="center")
    
10. To check the alias definition, run the command:
    
    ```bash
    git config --global --get alias.lg
    ```
    
    ![](https://i.imgur.com/O6yXu9f.png align="center")
    

### 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:
    
    ```bash
    git log --author="John Doe\|Alice"
    ```
    
    ![](https://i.imgur.com/jPFSz8f.png align="center")
    
2. To search for commit by date, run:
    
    ```bash
    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:
    
    ```bash
    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".
    
    ![](https://i.imgur.com/MdwVz5P.png align="center")
    
4. To search for commits that modified several files, run:
    
    ```bash
    git log -- testfile-01 branchfile-01
    ```
    
    This will show commits affecting *testfile-01* and *branchfile-01*.
    
    ![](https://imgur.com/fKbEBc1.png align="center")
    
    <div data-node-type="callout">
    <div data-node-type="callout-emoji">💡</div>
    <div data-node-type="callout-text">Using the <code>--</code> we inform git that we have files in mind, not branches.</div>
    </div>
    
5. To show the last 5 commits, run:
    
    ```bash
    git log -5
    ```
    
    ![](https://i.imgur.com/t7OA3nf.png align="center")
    
6. To list only merge commits, run:
    
    ```bash
    git log --merges
    git log --no-merges
    ```
    
    * `--no-merges` - excludes merge commits from the output, run:
        
    
    ![](https://i.imgur.com/t6Cw3cc.png align="center")
    
7. To search for commits from a specific branch, run:
    
    ```bash
    git log main
    git log master..second-branch
    git log second-branch..master
    ```
    
    ![](https://i.imgur.com/PKmiB41.png align="center")
    
    `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.
    
8. To get a precise result, we can combine multiple filters:
    
    ```bash
    git log --author="John Doe" --grep="bug-fix" --since="10 weeks ago"
    ```
    

## References

1. [git log documentation](https://git-scm.com/docs/git-log)
