Skip to content
DeveloperMemos

Using git log to View a Git History

Git, Version Control2 min read

One of the key advantages of using a version control system like Git is the ability to track and review changes made to a codebase over time. The git log command offers a powerful way to access the commit history of a repository, allowing developers to understand who made what changes and when. In this article, we will delve into the usage of git log and highlight some useful examples to effectively navigate through a Git history.

Basic Usage

To start exploring the Git history of a repository, open your terminal or command prompt and navigate to the root directory of your project. Then, simply enter the following command:

1git log

By default, this command will display the commit history in reverse chronological order, listing the most recent commits first. Each commit entry provides information such as the author, date, and commit message.

Filtering the Commit History

Limiting the Number of Commits

If the commit history is extensive or you only want to view a specific number of commits, you can use the -n option followed by the desired number of commits. For example, to display the last three commits, use the command:

1git log -n 3

Restricting the Output to a Specific Branch

To narrow down the commit history to a particular branch, use the --branches=<branch-name> option. This displays only the commits belonging to the specified branch. Replace <branch-name> with the name of your desired branch. For instance:

1git log --branches=main

Filtering Commits by Author

Sometimes, you may be interested in viewing the commits made by a specific author. You can achieve this using the --author flag followed by the author's name or email address. Here's an example:

1git log --author="John Doe"

Customizing the Output

Formatting the Commit Log

By default, git log presents each commit on a separate line, displaying key information like the commit hash, author, date, and message. However, you can customize the output format using the --pretty option.

For instance, to display each commit on a single line with concise details, use the command:

1git log --pretty=oneline

Additionally, Git provides various placeholders that allow you to create a custom log format tailored to your needs. Some commonly used placeholders include:

  • %H: Commit hash
  • %an: Author name
  • %ad: Author date
  • %s: Subject (commit message)

To utilize these placeholders, specify the desired format string with the --pretty=format option. Here's an example:

1git log --pretty=format:"%h - %an, %ad : %s"

Graphical Representation of Commit History

To visualize the commit history as a textual graph, indicating branch paths and merges, you can use the --graph option. This creates a more comprehensive view of the repository's development timeline. Combine it with other options for better insights, like so:

1git log --graph --all --decorate --oneline

In conclusion, the git log command is an invaluable tool for examining the history of a Git repository. By leveraging its various options and filters, you can efficiently navigate through the commit log, filter by specific criteria, and customize the output to suit your needs. Understanding how to effectively use git log provides valuable insights into the development timeline and aids in collaboration within a team.