Skip to content
Related Articles

Related Articles

How to Check Git Logs?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 13 Jan, 2022

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 and 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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!