The git log
command helps you to view the history of commits in the repository. In this guide, we'll focus on a specific use of the git log
command—git log -1
—which is used to display only the most recent commit.
Understanding the git log command
Before exploring git log -1
, let's first understand the git log
command. git log
displays the commit logs. The output includes the ID, author, date, and message for each commit. By default, git log
lists all the commits starting with the most recent.
Using git log -1 to view the latest commit
The syntax git log -1
uses a shorthand option to modify the output of the git log
command. This option limits the number of commit entries returned by the command. Specifically, -1
tells Git to show only the most recent commit. This is useful when you want a quick look at the last commit without scrolling through an extensive commit history.
This is a shorthand for --max-count=1
, which limits the output to just one commit. The number following the dash can be changed to any integer to specify the exact number of commits you want to see. For example, git log -3
would show the three most recent commits.
This option is part of a broader set of filtering and formatting options available with git log
that can help you customize the output to better suit your needs, such as limiting the number of commits, restricting the commits to certain authors, or modifying the display format.
Output
The output will show the most recent commit's details, including:
- Commit hash: A unique identifier for the commit.
- Author: The name and email of the user who made the commit.
- Date: The date and time of the commit.
- Commit message: A brief description of the changes made in the commit.
Advanced usage of git log -1
You can combine git log -1
with other options to customize the output.
Formatting the output
If you want a more concise output, you can use the --pretty=format:"%h - %s"
option, where %h
is the abbreviated commit hash and %s
is the subject (commit message). This will display the latest commit in a single line, which addresses the use case for the keyword "git log 1 line".
Example command:
git log -1 --pretty=format:"%h - %s"
Filtering by author
To see the latest commit by a specific author, use the --author
option:
git log -1 --author="Author Name"
Showing changes made in the latest commit
To see the actual changes (diff) made in the latest commit, use the -p
option:
git log -1 -p
This will display the diff of the latest commit along with the commit details.
For further reading see the official Git documentation.