The Wayback Machine - https://web.archive.org/web/20240625000558/https://www.geeksforgeeks.org/how-to-check-git-logs/
Open In App

How to Check Git Logs?

Last Updated : 13 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Git is the most popular version control system which records the changes made to our project over time in a special database called a repository. We can look at our project and see who has made what changes when and why and if we screw something up we can easily revert our project back to an earlier state. In this article, we will see various commands used for checking git logs i.e history of a repository or project you can say what are the core meaning of these commands, and what are their use cases which command is used in which scenario.

Key Terminologies:

  • Log: It is a record of all the commits done in the repository.
  • Commit: A commit is a snapshot of the git repository at one point in time.
  • Commit id: It is a 40-character hexadecimal value and it’s a unique identifier that git generates every time we make a commit to our repository.

So let’s start this article. The most famous and common command used to browse history using git is git logs. So now let’s take a deep dive into what git log is and how it works.

Git log

It displays all the commits being made in that repository in multiple lines along with the commit id, author name, date and commit message. If you have multiple logs in git log so to exit from the git log command press q. So now if you want to limit your logs to n commits. For example, if you want to see only 1 commit in your git log or you want to see 2 commits or it can be any number depending on the total number of commits you have done in your git repository. The command for that would be git log -n where n represents the number up to which commit you to want to see the logs.

Using git log

Passing a parameter in git log so that it would last to n commit

Prettier log:

To see the logs in a very decorated, neat, and clean manner we use the following command.

git log --decorate --oneline --graph

Since it’s a pretty big command we can assign an alias to this command

git config --global alias.name_of_alias "command_name"

So now let’s break this command into parts git config –global this is used so that the alias would be globally recognized as a command in git alias.name_of_alias is used to give alias a name and then the “command_name” is the command of which you are making an alias and this command should be written in double-quotes. Now to use the alias of the command type in git alias_name  and if you want to see the history of everything in your repository pass in –all as a parameter in the command.

Using the command git log –decorate –oneline –graph

Making an alias of the command git log –decorate –oneline –graph

Using the alias of the command 

Checking the history of everything in the repository

Colorize logs:

To see the logs in a colorized fashion or to customize the  log output format we use the below command

git log --graph ---pretty=format: '%C(red)%h%Creset  -%C(yellow)%d%Creset %s %C(green)(%cr)%C(yellow)<%an>%Creset'

So now let’s break down this command into parts to get the core meaning of it. Here you can see the format option which allows us to customize our own log output format. There are a lot of parameters in this command so let’s understand them in a tabular fashion here I have made a table with the details of each parameter.

Parameter

Details

%C colors the output that comes after it
%h denotes the first some letters of the commit ID 
%H denotes the complete commit id
%Creset resets color to the default terminal color
%d ref names or simple names
%s denotes the commit message
%cr committer date
%an author-name

Using the above command to customize our own log output format

We can also make an alias of this command in order to avoid writing this big command. The command used for creating the alias would be the same as we used in creating an alias for displaying the log output format in a decorated manner. The command used is: 

git config --global alias.alias_name "Command_name"

using the git alias_name 

Oneline log: 

This will display the log outputs in oneline. But it only shows first some parts of the commit id and the commit message.

Using git log –oneline and git log online -n

So now if you want to limit your logs to n commits. For example, if you want to see only 1 commit in your git log or you want to see 2 commits or it can be any number depending on the total number of commits you have done in your git repository. The command for that would be git log –oneline -n where n represents the number up to which commit you to want to see the logs.

LogSearch: 

So what log search does it actually matches the text written in our file with the commit id so that we get to know when the data in the file was this string say hello then which commit ids were matching with this string. So there are some parameters or options in log search which make our searching easy.  So let’s explore them one by one.

  • git log -S string: Here, S stands for searches and string is a group of characters i.e some text for e.g:- hello cloud this all statement is a string. So this command displays the most recent commit id associated with the string. But if you want to see all those commit ids associated with this particular string then we use a different parameter in this command.
  • git log -G string: Here G stands for a group it shows all the commit ids associated with the string.

Using git log -s string and git log -g string

git shortlog: Basically it displays the log output format in the following syntax:

Committer1(number of commits)

  • Commit Message 1
  • Commit Message  2
  • Commit Message n

where n represents the last commit message number committed by the author 

Committer2(number of commits)

  • Commit Message 1
  • Commit Message  2
  • Commit Message n

where n represents the last commit message number committed by the author

Using git shortlog

Here you can see only my name, number of commits, and commit messages of those commits because in my case I have only worked in my repository that’s why it is showing my name, number of commits, and commit messages associated with it. But in big projects, there are a lot of developers working on a single project, and whenever they modify something or make any changes in the project they commit those changes then there you can see a lot of committers along with their number of commits and commit messages. In the git shortlog also there are a number of parameters or options available in the command so let’s understand them.

Parameter

Details

-s or –summary It displays the number of commits and the committer’s name
-e or –email It displays the committer’s name, number of commits, commit messages along with the mail id
-n or –numbered It sorts the output by the number of commits instead of alphabetically by committer’s name

Using gitshortlog -s ,git shortlog -e , git shortlog -n

git grep

So how git grep works is if you want to fetch where a particular string exists in a file or whether a particular string is associated with a particular commit id. In such cases git grep is a very useful command. Just you have to pass a particular string in the command when checking for the existence of a  string in a file and when checking for a particular string in a commit id mention the string in double quotes.

Using git grep

For a range of lines within a file: The command would be the same git log just some new parameters we have to pass in it for checking for a range of lines within a file. The command is: 

git log -L starting_line_number,ending_line_number:file_name here -L denotes the line range for a file.

Using git log -L starting_line_number,ending_line_number:file_name

Filter logs:

In this concept, we will see various parameters used in the command to filter logs. 

  • For some days: git log –after ‘mention the days ago’ 
  • for e.g:  git log –after ‘4 days ago’
  • For specific dates: git log –after year_number-month_number-date_number
  • For Author name: git log –author=”Author_name”

We can also use the since clause instead of after in this command.

Using git log –after ‘number_of_days days ago’

Using git log –after year_number-month_number-Date_number

Using git log –author=”author-name”

Changes inline:  

If our use-case is to see the logs with changes inline we use a parameter -p or –patch to see the logs with changes inline.

Using git log –patch

Committed files: 

If you want to see the files in logs that are being committed by commits. we use the command 

git log --stat

Using git log –stat

Contents of a Commit:  

ow if you want to view the contents of a commit you can use a simple command

git show commit-id

Using git show  commit-id

Committer name and time since commit

Now if you want to see the committer name and time since he committed in oneline. We use the  following command

git log --oneline --decorate --source --pretty=format:'"%Cblue %h %Cgreen %ar %Cblue %an %C(yellow) %d %Creset %s"' --all --graph

The output of the above command



Previous Article
Next Article

Similar Reads

Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
How to Analyze Threats in Apache Logs?
Apache HTTP server is the most widely used web server. it generates valuable log files that contains monitored server activity and identified potential security threats. This article will provide you with a step-by-step guide to analysing Apache logs for threats, using some practical examples and tools. Apache Log FilesApache primarily generates tw
3 min read
How to Create API to View Logs in Node.js ?
Log files are essential for monitoring and troubleshooting applications. They provide insight into the application’s behaviour and help identify issues. In a Node.js application, you might want to create an API that allows you to view these logs easily. This can be particularly useful for centralized log management, debugging, and remote access to
5 min read
Difference between “git add -A” and “git add”
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. It is basically, software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Featu
3 min read
Git - Difference Between Git Revert, Checkout and Reset
While Working with Git in certain situations we want to undo changes in the working area or index area, sometimes remove commits locally or remotely and we need to reverse those changes. There are 3 different ways in which we can undo the changes in our repository, these are git reset, git checkout, and git revert. git checkout and git reset in fac
7 min read
Git - git-show Command Line Utility
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be
3 min read
Git LFS: Managing Large Files in Git Repositories
Git, undoubtedly one of the most popular version control systems, is widely used for managing source code in software development projects. However, one of its limitations is its handling of large files. Traditional Git repositories struggle to efficiently manage large files, leading to bloated repositories and slow performance. This is where Git L
4 min read
Git Subtree vs. Git Submodule
Git Subtree and Git Submodule are both mechanisms in Git that allow you to incorporate external repositories into your own repository. They provide a way to manage dependencies and include code from other projects while keeping your repository organized and maintainable. Git Subtree: Git Subtree allows you to insert a separate repository as a subdi
4 min read
Difference Between Git Fetch and Git Pull
Understanding the difference between git fetch and git pull is important for effective version control in Git. These commands are important for managing your repository and collaborating with team members. In this article, Let us look at Git Fetch and Git Pull separately with the help of an example. What is Git Fetch?The Git Fetch command is used t
4 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
Article Tags :
three90RightbarBannerImg