📜  git log show last n commits - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:41:26.526000             🧑  作者: Mango

Git Log Show Last N Commits - Shell/Bash

Introduction

In Git, the 'git log' command is used to display the commit history of a repository. With this command, you can view information about each commit such as the commit message, author, date, and time. Moreover, it is possible to limit the amount of information displayed by adding various parameters.

In this article, we will show you how to use the 'git log' command to show the last n commits of a Git repository using Shell/Bash.

Syntax

The basic syntax of the 'git log' command is as follows:

git log [options] [<revision range>] [[--] <path>…]

where:

  • 'options' are various parameters or options that can be used to customize the output of the command.
  • '' specifies the range of commits to be displayed. It can be a commit hash, a branch name, a tag name, or a number of commits.
  • '' specifies the path to a file or directory whose commit history is to be displayed.
Show Last N Commits

To show the last n commits of a Git repository, you can use the following command:

git log -n <n>

where:

  • '' is the number of commits to display. For example, 'git log -n 5' will show the last 5 commits.

This command will display the commit history of the repository, starting from the most recent commit and going back n commits. By default, the output will show the commit hash, author, date, and commit message of each commit.

More Options

There are several options that can be used with the 'git log' command to customize the output. Some commonly used options are:

  • '--pretty=format:' - This option can be used to format the output of the command. For example, 'git log --pretty=format:"%h %an %ad %s"' will display the commit hash, author name, commit date, and commit message.
  • '--abbrev-commit' - This option abbreviates the commit hash to 7 characters, which is useful when viewing long commit histories.
  • '--graph' - This option displays a text-based graph of the commit history, showing the relationships between commits and branches.
  • '--oneline' - This option displays each commit on a single line, which is useful when viewing a large number of commits.

For more options, you can refer to the Git documentation.

Conclusion

In this article, we have shown you how to use the 'git log' command to show the last n commits of a Git repository using Shell/Bash. With this command, you can view information about each commit, limit the amount of information displayed, and customize the output using various options.