Git is a powerful version control system that allows developers to track changes to their codebase over time. The “git log” command is used to view the commit history of a Git repository. This command is essential for navigating through the project’s history and understanding the evolution of the codebase.
Here are several examples demonstrating the usage of git log
:
Example 1: List all commits in the repository:
git log
This command displays a detailed list of commits, showing commit hashes, authors, dates, and commit messages. Each commit is listed chronologically from the most recent to the oldest.
Example 2: Limit the number of commits shown:
git log -n 5
By specifying -n 5
, only the latest 5 commits are displayed. This is useful when you only need to see recent changes without overwhelming detail.
Example 3: Display a compact summary of each commit:
git log --oneline
The --oneline
option condenses each commit to a single line, showing abbreviated commit hashes and commit messages. This format is handy for a quick overview of commits.
Example 4: Show changes introduced by each commit:
git log -p
Adding the -p
flag displays the patch representing each commit. This includes the changes made to files, providing a detailed view of what was modified.
Example 5: Filter commits by a specific author:
git log --author="John Doe"
Using --author="John Doe"
filters the commit history to show only the commits authored by John Doe. Replace “John Doe” with the actual author’s name.
Example 6: View commits within a specific time range:
git log --since="2023-01-01" --until="2023-12-31"
This command restricts the commit history to those made between January 1, 2023, and December 31, 2023. Adjust the dates as needed.
Example 7: Show only merge commits:
git log --merges
--merges
option filters the log to display only merge commits, which are commits that result from merging branches.
Example 8: Display commits affecting a specific file:
git log --
Replace <filename>
with the actual filename. This command shows only the commits that modified the specified file.
Example 9: View commits using a graphical interface:
git log --graph --oneline --all
Combining --graph
with --oneline
and --all
options provides a visual representation of the commit history, showing branches and merges.
Example 10: Search commit history by commit message:
git log --grep="bug fix"
Using --grep="bug fix"
filters the commit history to show only the commits whose messages contain “bug fix”. Adjust the search phrase as needed.
Verification Steps: After executing any of the above git log
commands, you can verify its execution by examining the terminal output. Ensure that the output corresponds to the expected result based on the command used:
- Check if the number of commits matches the expected count for commands like
git log -n 5
. - Verify if the filtered commits (by author, file, date range, etc.) are correctly displayed.
- Compare the graphical representation (if applicable) with the branch structure in the repository.
Discussion about this post