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