Git Log Command: Distributed version control software like Git helps individuals and teams manage their source code by taking snapshots of changes and bundling them into commits and repositories.
This allows developers to explore old configurations of code, which can be helpful for debugging or adding new features. Knowing when or who made a change is also beneficial.
Git allows you to access “meta” data for each commit, including the author, references to other commits, reasons for changes, and how those changes affected the code. This is called logging, and we’ll be talking about it today.
Post On: | GIT Log Command |
Post Type: | GIT Tutorial |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
In this blog post about Git log, we are going to discuss the following:
- What is Git Log?
- Git command to view commit history
- Git command to view commit history for a file
- Git command to view the commit history of a range
- Git command to view a limited number of commit history
What is Git Log Command?
The git log command is used to display a list of all commits made to a repository. This list includes the committed author, date, and message. You can execute this command by entering the git log.
The above text displays a commit history with various other information included. The commits are displayed in reverse chronological order, with the most recent commit appearing at the top. This is useful for Git repositories when more than one person uses it.
When a user executes the git log command, then you can get four important pieces of information, and those are:
Add one git commit history, and in each section, paste the information
- Commit Hash: When you’re looking at a commit in Git, the first part of the commit log is always the “commit hash.” If you’re not sure what a commit hash is, don’t worry! It’s just a string of characters that uniquely identify a commit. Git uses these hashes to reference commits internally, so it’s important to notice them in the Git log.
- Commit Author: This section provides information about who made the changes to the repository, including the author’s name.
- Commit Date: This part of the text displays the date and time of the commit, including the day and time zone. It’s helpful to have this information included because people working on a project from different geographical locations can refer to it.
- Commit Message: This section displays the commit message written when the changes were committed.
When there are lots of commits on your repository, some users don’t want to see the huge list of git commits. That’s why Git log provides many options that help you filter out the commit history according to your preference, giving you some power over what is displayed.
How to Exit the git log Command?
If you ever find yourself in a situation where you’re stuck in all the commands, don’t worry! There’s an easy way to get back to bash. Just hit the Enter key, and you’ll be navigated to the older command until you reach the end flag (end).
If you ever find yourself in such a condition, the solution is to press q (Q for quit). This will exit you from the situation and take you back to the command line. From there, you can perform any of the commands.
Git Log Oneline
This option displays the output as one commit per line, which can be helpful in seeing a concise view of the data. Additionally, it shows the output in brief, like the first seven characters of the commit SHA and the commit message.
You can use the command below:
git log –oneline
The view of the text has changed so that fewer details are visible, and it is now only possible to see one line at a time. This makes sense because each commit is now shown in a single line, with the commit ID followed by the commit message.
Commit Id: Each commit has a shortened ID, which is a compressed form of the commit’s hash. Even though hashes are not sequential numbers, they can still be used to identify commits uniquely.
Commit Message: The commit message should be clear and concise so that you can understand the purpose of the commit.
Git Log Stat Command
The git log stat command helps you see which files have been modified and how many lines in each file were changed and provides a summary line of the total records updated.
You can execute the command by entering like below.”
git log –stat
Git log P or Patch
The git log patch command display is super handy because it shows the files that have been modified and provides the juicy details of where those modifications occurred. No more guessing which lines were added, removed, or updated!
git log –patch
The output above displays the files that have been modified and the line numbers of the added or removed lines.
Git Log Graph
This command allows you to see your commits in a graphical form, which is much easier to understand. You can visualize the rate of commits and other factors affecting your commits.
We type the following commands to view this.
$ git log –graph
$ git log –graph–oneline // gives details in separate lines
Filtering the Output of the git log
If you want to view a list of all the commits made to your repository, git log is the command for you. By default, it returns a list of all commits using this structure. While this can be helpful if there aren’t many commits, it can quickly become difficult to read if there are too many.
Let us find out the different options of git log:
Filter by Most Recent
If a number of commits are happening in a repository, then when you execute the command git log, the huge list will appear. So, to remove the complexity of this type of behavior, we can lower the commit list. To Display a number of commits, you can use the below command:
Git log -n 2 (Display last 2 commits)
Note: This will display the last-mentioned number of commits.
Filter by Author or Committer
Git is a distributed version control system, so in real-world projects, multiple developers and testers are working on it from various locations. In that case, suppose you want to list down all the commits by a particular team member, then we can use the –author flag to filter the commits by author name with the git log.
When this command executes, it takes a regular expression and returns the list of the commits which match the pattern. We can write the command below
git log –author=”Author Name”
git log –committer=”Author Name”
Note: You have to enter the exact name of the user instead of any pattern.
If you notice the above output, then you can see all the commits by the entered author name. Instead of the full sentence (Author name), you can use string also (first name).
Let us try to execute the below statement:
git log –author=”SoftwareTestingo”
git log –committer=”SoftwareTestingo”
Executing the above statement will list down all the users who have included the name. That means when you are entering the author’s name, it’s not mandatory to enter the exact match full name; you can specify the phrase also.
Note: The Entered Name is Case sensitive
If you see the commit details on Git bash, it also contains the author’s email id. We can also search with the email id. Suppose you want to track down all the commits whose email service provider is Outlook.
git log –author=”@email.com”
By Commit message:
We can filter the commits by commit message using the grep option, which will work similarly to the author option. You can write the git command like below:
git log –grep=” Commit message.”
Git also allows users to enter the short form of the commit message. So you can write the command below:
git log –grep=” file”
The above output displays all commits that contain the word commit in its commit message. This can be helpful when trying to find a specific commit or narrow your search.
Filter by Date
The Git log can be filtered by date using the –before and –after flags. These flags accept a wide range of date formats, but relative references and full dates are the two most commonly used.
If we want to get a list of commits after June 24, 2022, we could use this command:
git log –after=”2022-24-07″
We have set a date to filter our commits. If we want to get a list of commits from before yesterday, we can do so using this command:
git log –before=”yesterday”
Similarly, if you want to display commits between two days, then you can write your command like below
git log –after=”2022-7-24″ –before=”2022-7-27″
Git shortlog Command
The git shortlog command is a quick way to see who has made what changes to a repository. The output of the command groups the information by author, so you can easily see which person made which changes.
git shortlog
Conclusion
The git log command allows you to view the history of changes made to a repository, who made those changes, and when. You can use filters with the git log command to show only the information that you are interested in.
The git log command has two types of flags. Some help you format the output, while others can help filter commits returned by the command. See you at the next git tutorial.