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

How to Check Git Logs?

Last Updated : 13 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
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
How to Set Up Git Using Git Config?
Git is a powerful version control system that helps developers manage and track changes in their code. Setting up Git correctly is important for maintaining an organized and efficient workflow. In this article, we'll walk you through the process of setting up Git using the git config command. PrerequisitesBefore we dive into the setup process, make
3 min read
How To Exit Git Log or Git Diff?
When working with Git, the git log and git diff commands are important for viewing commit history and changes between commits, respectively. However, if you're new to Git, you might find yourself stuck in these views, unsure how to exit. This article will guide you through the steps to exit git log or git diff, ensuring a smooth experience while na
2 min read
Git - git prune
Git is a powerful version control system that helps developers manage their code efficiently. However, as your project evolves, you may accumulate unnecessary data that clutters your repository. This is where git prune comes in view. What is git prune?git prune is a Git command that removes objects that are no longer referenced by any branch or tag
5 min read
Difference between “git add -A” and “git add”
When working with Git, the git add command is used to move changes from your working directory to the staging area. However, there are different options and flags that can be used with git add, such as -A, which might lead to some confusion. In this article, we'll explore the differences between git add -A and git add. 1. Git add- CommandThe git ad
2 min read
Git - Difference Between Git Revert, Checkout and Reset
Git offers a range of commands to manage and manipulate your codebase. Among these commands, git revert, git checkout, and git reset are frequently used for different purposes. Understanding the differences between these commands is important for effective version control. In this article, we'll explore the functionalities and use cases of git reve
6 min read
How to Fix Git Error "Unable to create '/path/my_project/.git/index.lock'"?
The Git error "Unable to create '/path/my_project/.git/index.lock'" typically occurs when Git is unable to create or write to the index.lock file. This file is used to prevent simultaneous processes from modifying the repository, which could lead to corruption. Table of Content Remove the Lock File ManuallyUse Git Command to Remove the Lock FileRes
2 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
How to Check Branch in Git?
In Git, branches are independent lines of development that allow you to work on features or fixes without affecting the main codebase. Checking branches is important for managing project history, coordinating collaboration, and ensuring smooth development workflows. This article will walk you through the various approaches and commands to check bra
2 min read
How to Check Remote Origin URL of a Local Git Repository?
In Git, remote repositories serve as centralized resources where code is stored and shared among team members. When working with a local Git repository, it's important to know the URL of its remote origin, as it determines where changes are pushed and pulled from. In this article, we'll learn how to check the remote origin URL of a local Git reposi
2 min read
An Ultimate Guide to Git and Github
Highlights of the article: Introduction to GitGit Repository StructureGithubAccessing Github central repository via HTTPS or sshWorking with git - Important Git commandsIntroduction to Git For installation purposes on ubuntu, you can refer to this article: How to Install, Configure and Use GIT on Ubuntu? Git is a distributed version control system.
8 min read
Git - Changing History
Git provides a very important option of altering the commit history. As the master of git, you can control and make it function according to you. Git gives you the authority to change the order of commits, change messages, remove commits entirely. Moreover, it also allows you to manage the project's history. However, it is advisable to commit the w
3 min read
Introduction to Git Branch
Branching means diverging from the mainline and continue to work separately without messing with the mainline. Nearly every VCS has some form of branch support. In Git, a branch is simply a reference to the commit, where the following commits will be attached. Git vs SVN: In recent years, the use of git has risen tremendously. Unlike SVN, git allow
3 min read
Git Features
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 which may b
9 min read
Bare Repositories in Git
Repositories in Git are a snapshot of the folder in which you are working on your project. You can track the progress and changes made to the project by making commits and also revert changes if not satisfactory. Repositories can be divided into two types based on the usage on a server. These are: Non-bare Repositories Bare Repositories What is a N
7 min read
Getting changes from a Git Repository
Git allows performing various operations on the Repositories including the local repositories and remote repositories. The user when downloads a project to work upon, a local repository is created to store a copy of the original project. This local repository stores the changes that are being made by the user. This way, the user can edit the change
4 min read
Git | Working with Stash
Pre-requisites: Git Git allows multiple users to work on the same project simultaneously. Suppose a developer is working on a feature in a branch and he needs to pull changes from some other developer's branch or if he has to work urgently on some other feature, but the feature he is currently working on is incomplete. In this case, you cannot comm
8 min read
Handling repositories with Git remote
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. Syncing ability of the git allows the developers to access and work on any project anytime anywhere. The collaboration among developers via git takes place when git provides each individual deve
4 min read
Using Git on CommandLine
Git is very important and powerful tool when it comes to the software development life cycle. Now in this article, we are going to see why git is used vastly in the software industry and what kinds of operations we can perform with git. So git is basically a distributed version control system for tracking changes in source code during software deve
5 min read
Working on BitBucket using Git
Git provides various tools and applications to perform better handling of the work environment. Git is a type of version control system that is used to allow more than one developer to access the source code of a specific application and can modify changes to it that may be seen by other developers. BitBucket is a git-based tool that helps with col
5 min read
Working on Git for GUI
Git has its native environment within the terminal. All the new features are updated first at the command line, and only there is the full power of Git. But plain text isn't the simplest choice for all tasks; sometimes some users are much more comfortable with a point-and-click interface, a visual representation is what they need. Note: There's not
4 min read
Git Rebase
Pre-requisites: Git Git rebase can integrate the changes from one branch to another by overcoming the problems that we might have faced while using the git merge command. The changes we will do will be recorded in the form of logs which are useful to go through if any mistakes happen. What is Branching?Branching means splitting from the master bran
12 min read
Recovering Lost Commits in Git
Git is a very powerful tool, and with great power comes great responsibilities. If not used and handles properly, It might cause you to lose your commits. There might be situations when you may find all of your work missing at once. If you have regularly committed your work, there is a way to recover these lost commits. This tutorial will aim at us
2 min read
Article Tags :