Git Log

Learn via video courses
Topics Covered

Overview

The git log command shows us snapshots of the commits or the record of all the commits of the git repository. The git log command is one of the most commonly used Git commands or a utility tool that displays all the history of the current git repository. The git log command also helps us to search for particular commits but the command only works on the committed history of the project. The git log command can be compared with the git status command. We can always use the git status and git log commands to get details of the previous commits.

Pre-requisites

The prerequisites for learning the git log command can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git log command.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems: Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state that is known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Branching

A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

GitHub

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert ba a certain state of the project (as GitHub saves the history of the project). GitHub supports open-source development where several developers can collaborate and support each other in the development process.

Anyone can create an account on the GitHub platform to host their code, files, and documents. GitHub sells hosted private code repositories, and other collaborative business model plans to make money as an organization.

What is Git Log?

The git log command is one of the most commonly used Git commands or a utility tool that displays all the history of the current git repository. The git log command shows us snapshots of the commits or the record of all the commits of the git repository. Now, what is a commit? Well, a commit is a snapshot of the project's current version(s). So, we track these commits and can revert to a certain commit if we want. Git tracks the changes in a project and saves a certain state that is known as commit.

The git log command displays the following information about the repository:

  • Secure Hash Algorithm (SHA) value: It is 40-character checksum data that is generated using the SHA algorithm. It is also known as commit has which is always a unique number.
  • author data: It is metadata that shows the name of the author and the email address of the author.
  • commit date: The git log command also shows us the timestamp of the commit.
  • commit title and message of each commit of the project.

In simpler terms, we can say that the git log command is used for listing and filtering the history of the project. The git log command also helps us to search for particular commits. This command only works on the committed history of the project.

Now usually when we run the git log command, we get stuck on the screen. So, if we want to get back to the Git bash then we need to press q (which means quit) to exit from the frozen screen. Now if we want to navigate on the frozen screen then we can use the following keys:

  • k: It is used to move up on the screen.
  • j: It is used to move down the screen.
  • space bar: It is used to scroll down by a full page and to scroll up by a page.

The git log command can be compared with the git status command. The git status command is used to check the status of the repository. The git status command shows us a large view status of the git repository. We can always use the git status and git log commands to get details of the previous commits.

Usage

Let us now learn about various uses of the git log command and the various configurations of the git log command.

The main usage of the git log command is to examine the history of the git repository apart from that the git log command is used to find the particular version of the current project. We can personalize the output of the git command according to our preference. Let us now learn about the various configurations and filtration used with the git log command.

CommandOutput
git logIt is the by-default command that shows the entire commit history. We can use the keys like j, k, and space bar. We can type q to exit from the screen.
git log -n <limit>We can use the limit flag along with the git log command to limit the number of commits.
`git log --onlineAs the name suggests the online flag is used to show useful information about the project's history in a single line.
git log --statThe stat flag is used to show the changed files along with the number of added or deleted lines apart from the original output of the git log command.
git log -pThe p flag is used to show the patch of each commit along with the full diff.
git log --author="<pattern>"This command will search the commits with the specified author. Here, the <pattern> part can be a regex expression or a string.
git log --grep="<pattern>"This command will search the commits with the specified commit message. Here, the <pattern> part can be a regex expression or a string.
git log <since>..<until>This command will show the commits starting from <since> and ranging up to <until>. Here, <since> and until> can be a commit ID, branch name, HEAD, or any other type of reference.
git log <file>This command is used to display the commits that include the specified files which makes it easier to see the file’s history.
git log --graph --decorateThis command is used to display a text-based graph. The graph is shown on the left-hand side of the commit message. Here the decorate flag is used to add the name of the branches or the tags of the currently displayed commits. Here, we can also add the online flag to see the output in one line.

Git Log Command Example

As we have discussed, the git log command is used to show the entire commit history, helps us to search for particular commits, etc.

The command is:

If we run the above command in a committed git repository the following output will be shown. git log command example1

In the above image, we have a git repository, and the git log command shows us the entire commits made on that particular repository along with that the name of the author, email address of the author, date and time of the commit, and the commit message.

Git Log Oneline

We can use several flags with the git log command to perform categorized output. The oneline flag is used along with the git log command to show useful information about the project's history in a single line.

The overall command for the same is:

If we run the above command in a committed git repository the following output will be shown. git log command example2 The --oneline flag displays the following:

  • one commit in one line.
  • the first 7 characters of the commit's SHA value.
  • the commit message.

We can type q to exit the screen if we are using the git log --oneline command.

Git Log Stat

Now if we want to see the files that have been modified then we can use the stat flag along with the git log command. This command also displayed the number of lines and the summary of the change (total number of lines) that have been changed or updated.

The overall command for the same is:

If we run the above command in a committed git repository the following output will be shown. git log command example3

The --stat flag displays the following:

  • a total number of modified numbers in a commit.
  • a total number of lines that have been removed or added.
  • the summary of the total number of records that have been changed.

We can type q to exit the screen if we are using the git log --stat command.

Git Log p or Patch

If we want to display all the modified files along with the location of the added, updated, and removed lines of the modified file then we can use the p or patch flag along with the git log command. The overall command for the same is:

Or

If we run the above command in a committed git repository the following output will be shown. git log command example4

The --patch or `-a flag displays the following:

  • the modified files.
  • the location of added and removed lines of the modified file.
  • the specific changes made on the files in green color.

We can type q to exit the screen if we are using the git log --patch or `git log -a command.

Git Log Graph

The git log --graph command is used to display a text-based graph. The graph is shown on the left-hand side of the commit message. In simpler terms, we can say that this command displays our commits in the form of a graph.

The overall command for the same is:

If we run the above command in a committed git repository the following output will be shown. git log graph1 We can also add the oneline flag with this command to view the result in a single line.

The overall command for the same is:

If we run the above command in a committed git repository the following output will be shown. git log graph2

Here we can also use the decorate flag which is used to add the name of the branches or the tags of the currently displayed commits. We can type q to exit the screen if we are using the git log --patch or git log -p command.

View the Specified Number of Commits by the Author

The git log command gives us a lot of flexibility. We can even use the git log command for filtering the output, we can specify the commits to be displayed by providing the SHA value, author name, time, etc. The overall format of the command is:

Let us take some situations to understand the filtering better.

  1. If we want to view the latest commit (for example the last 3 commits), we can use the -n flag along with the git log command. Refer to the image provided below for more clarity. git log graph3

  2. If we want to filter the commits based on the name of the author or the basis of the name of the committer, we can use the --author flag and the --committer flag. Refer to the image provided below for more clarity. git log graph4

  3. We can also specify the date and time of the commit (start date of the commits and the end date of the commits) with the help of the --before and --after flags. Refer to the image provided below for more clarity. git log graph5

  4. Apart from these, we can also filter the output based on:

    • Filter by File: If we want to view the list of all the commits associated with a specific file, we can use the command git log "--file_name"
    • Filter by Content: If we want to filter the files and commits having a certain content then we can use the command: git log -S"#content".
    • Filter by Range: We can even filter the commits by specifying the range of the commit. The git log <since>..<until> command will show the commits starting from <since> and ranging up to <until>. Here, <since> and <until> can be a commit ID, branch name, HEAD, or any other type of reference.
    • Filter by Message of the commit: We can also filter the commits based on the commit message. We can use the --grep flag to search certain commit flags. the overall command for the same is: git log --grep=" message:".

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base, etc.
  • The git log command shows us snapshots of the commits or the record of all the commits of the git repository. The git log command can be compared with the git status command.
  • The git log command is one of the most commonly used Git commands or a utility tool that displays all the history of the current git repository.
  • The git log command also helps us to search for particular commits but the command only works on the committed history of the project.
  • The git log command displays the following information about the repository:
    • The 40-character checksum data or Secure Hash Algorithm (SHA) value.
    • The author data i.e. email address and name of the author.
    • commit date or the timestamp of the commit.
    • title and message of each commit of the project.
  • The main usage of the git log command is to examine the history of the git repository apart from that the git log command is used to find the particular version of the current project.
  • The oneline flag is used along with the git log command to show the useful information of the project's history in a single line.
  • If we want to see the files that have been modified then we can use the stat flag along with the git log command.
  • If we want to display all the modified files along with the location of the added, updated, and removed lines of the modified file then we can use the p or patch flag along with the git log command.
  • The git log --graph command is used to display a text-based graph. The graph is shown on the left-hand side of the commit message.