DEV Community

anis dabdi
anis dabdi

Posted on

10 helpful flags to use with git log command.

I’m part of the Cloud DevOps Bertelesman Udacity phase 1 scholarship program, and I’m participating in one of the optional challenges, the blog challenge which is to write a blog about one of the course Lessons, and I decided to write this blog about logging in git (VCS).

In this guide you’ll advance your capabilities of searching in the history of your repositories while working on your own projects, this will help a lot with tracking your changes and your teammate's changes as well.

We know that when it comes to logging or let’s say log files, it’s all about investigating or searching in the history for sure of your system or program that you’re working on, to know what’s going on with your stuff and check the current state of your system or program.

NB: this guide will be as short as possible, so I will list just git commands with flags, and a short description to give you an overview of what this flag does, in this guide, we’re assuming master as our main branch, so whenever I talk about something it’s from master main branch perspective, let’s get started.

  • git log

This command is the base of everything we will discuss here in this guide, this command lists all the commits you have in your repository with the following information (commit SHA, date, author email, username and commit message) but in the current branch you’re in, let’s say master branch.

  • git log --oneline

Alt Text

The --oneline flag lists all the commits as git log does, but just in one line with the first 7 characters SHA + the commit message as shown above in the image.

  • git log --all

Alt Text

This command lists all the commits that are not yet merged in our main branch, usually from other local branches, current branch commits + other branches commits, as you see over there in the image, there is a commit listed from the feature-branch even if we're just in master branch.

  • git log --merges

Alt Text

This command lists just the merged commits, which means just the commits coming from merge conflicts. Like if feature-branch gets merged into the master branch, conflict should be resolved before completing the merge, and a merge commit is added to the commits that state the conflict resolved between the content of same commits from different branches, these kind of commits will be listed alone using this command. it has its opposite command which is --no-merges which do the opposite and lists all none merge commits, as you see in the image below the above merge commit doesn't appear here with the opposite --no-merge flag.

Alt Text

  • git log --patch

Alt Text

There is a shorthand for this command which is –p, what this flag actually does is just listing the commits as git log does, but with the changes happened in this commit as well, the + sign means the additional lines of code added to the file/s in this commit, – sign shows the removed parts of code from the file/s in this commit and the white text means it's kept as it is, no changes happen to it.

  • git log --stat

Alt Text

Stat stands for statistics, it lists all the commits with the files and the exact number of changes that happened in each file.

  • git log –S”<string>”

Alt Text

This flag is wonderful, it helps you to search for a specific text inside your files, let’s say you search about some names you put on an HTML file and you want to know in how many commits these names reside.

  • git log --grep=”value here”

Alt Text

This flag helps a lot if you want to search for a specific commit in terms of a commit message, just put your piece of text you want to search for in commit messages and all the commits match the piece of text you provided will be listed.

  • git log --author=”username or email”

Alt Text

Sometimes you might want filtering commits by author username or email because author here means his username or his email, so you could put one of them in between quotes as a string value, you can also search for two authors in the same time with OR operator in the following format:

git log --author=”username1\|username2”

We used slash here as an escape character, because we're dealing with a string and this last one should be provided or the OR | operator will be considered as one character of the whole username, as in any programming languages like JavaScript especially in RegExp.

  • git log -<n>

Alt Text

should be replaced by a number in order to list the most recent commits, consider the following command git log -3, this one will list the most three recent commits.

  • git log -- file1

Alt Text

You can also list commits by files, for example, git log -- index.html you can add multiple files, git log -- index.html script.sh, and this last command lists all the commits that track the changes in these two files index.html and script.html.

All the above flags work together, so combine them whenever you need the output in such a desired format.

I hope you enjoyed this blog and it helps you to expand your knowledge in git.

Top comments (1)

Collapse
 
ulrichflynn profile image
UlrichFlynn

Thank you very much . It really helps me improve my skills in Git.