Viewing and Navigating Commit History

In Git, the commit history provides valuable insights into the evolution of a project.

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

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.

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.

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.

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:

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.

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:

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.

git log --graph

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.

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.

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.

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

git log --grep="bug fix"

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

Checking out a specific commit

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.

git checkout <commit-hash>

Creating a branch from a specific commit

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.

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

Comparing commits

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.

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.

git log HEAD~3..HEAD

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

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.

Last updated