# Viewing and Navigating Commit History

The `git log` command allows you to view this history in various ways, enabling you to understand when changes were made, who made them, and what exactly was changed. In this section, we will explore the `git log` command and its options for displaying commit history.

#### Basic `git log` command&#x20;

To start with, you can simply run `git log` in your terminal to view the commit history of the current branch. By default, it shows a chronological list of commits, starting with the most recent one.

```bash
git log
```

#### Limiting the number of commits

If the commit history is extensive, you may want to limit the number of commits displayed. The `--max-count` option allows you to specify the maximum number of commits to show.

```bash
git log --max-count=5
```

#### Displaying commit details

The default `git log` output shows each commit's SHA-1 hash, author, date, and commit message. However, you can customise the log format to display additional information such as the commit's diff.

```bash
git log --stat
```

#### Formatting the output

Git provides flexible options to format the output of `git log`. You can use placeholders to include specific information, such as the commit hash (`%H`), author name (`%an`), commit message subject (`%s`), etc. Here's an example that displays a custom log format:

```bash
git log --pretty=format:"%h - %an, %ar : %s"
```

For more information on customising the log format, you can refer to the [Git documentation on pretty formats](https://git-scm.com/docs/pretty-formats).

***

### Exploring different log formats and filtering options

In this section, we will delve deeper into the log format options provided by Git and explore various filtering options to narrow down the commit history.

#### Graphical representation of commits:&#x20;

To visualise the branching and merging in your commit history, you can use the `--graph` option along with `git log`. This provides a more intuitive representation of the commit graph.

```bash
git log --graph
```

<figure><img src="https://3668347079-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrzbWIF3PxGJowb1Hlwat%2Fuploads%2F1USOCsAquBqKbnIoAnoa%2FScreenshot%202023-07-16%20at%2011.44.02%20AM.png?alt=media&#x26;token=fb129f67-f81f-450e-82cf-1ec12196ad59" alt=""><figcaption></figcaption></figure>

#### Showing the commit diff

To see the detailed changes made in each commit, you can use the `--patch` or `-p` option. It displays the diff associated with each commit.

```bash
git log -p
```

#### Filtering commits by author

If you're interested in viewing the commits made by a specific author, you can use the `--author` option followed by the author's name or email.

```bash
git log --author="John Doe"
```

#### Filtering commits by date range

Git allows you to filter commits based on a specific date range. The `--since` and `--until` options are used for this purpose.

```bash
git log --since="2023-01-01" --until="2023-06-01"
```

#### Searching for commits by keywords

To find commits containing specific keywords in the commit message, you can use the `--grep` option followed by the keyword(s).

```bash
git log --grep="bug fix"
```

### Navigating through commit history using Git commands

In this section, we will explore various Git commands that help you navigate through the commit history.

#### Checking out a specific commit&#x20;

To inspect the codebase as it was at a particular commit, you can use the `git checkout` command followed by the commit hash. This places your repository in a "detached HEAD" state.

```bash
git checkout <commit-hash>
```

#### Creating a branch from a specific commit&#x20;

If you want to create a branch starting from a specific commit, you can use the `git branch` command followed by the branch name and the commit hash.

```bash
git branch <branch-name> <commit-hash>
```

#### Comparing commits&#x20;

Git provides several ways to compare different commits. For example, you can use the `git diff` command followed by two commit hashes to see the differences between them.

```bash
git diff <commit-hash1> <commit-hash2>
```

#### Moving through commit history

Git allows you to move through the commit history using relative references. For instance, you can use `HEAD~1` to refer to the previous commit and `HEAD~2` for the commit before that.

```bash
git log HEAD~3..HEAD
```

This command displays the commit history from the commit three steps before `HEAD` to the current `HEAD`.

{% hint style="success" %}
By understanding how to view, format, and filter commit history using `git log`, as well as utilizing other Git commands for navigation, you gain a powerful toolset for exploring the evolution of your project and understanding the changes made over time.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitdeveloperguide.solomonmarvel.com/git-fundamentals/viewing-and-navigating-commit-history.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
