Git is a wonderful piece of software that makes life easier and more productive for a programmer. But sadly most developers don’t use it up to their full potential so we are going to blog about various simple and advanced usages of git as a series. In the first article of the series, we are going to talk about git logs.
Git log is a great feature, that allows us to keep track of our works. There are different options available under
git log command, which help us to customize the output of git log and also to filter the log.
git log --graph --decorate --oneline
The –graph option draws a graph representing the branches and its structure of commit history. –oneline is used to display commit message and its hash in a single line, which is used along with –decorate, which helps us to easily see which commit belongs to which branch.
git log --pretty=format:"<string>"This lets you display each commit however you want in printf style. For example
%cdwhich represents committer name, abbreviated hash and committer date respectively.
git log --pretty=format:"%cn committed %h on %cd"
Filtering the outputGit log can be filtered by different filters and format output
git log -<n>It displays latest n commits and its output. For example
git log -3Will display latest 3 commit details
git log --after=<date> git log --before=<date>Using
--before,we can get logs after or before the specified date. You can also use these both to get logs between two dates Example
git log --after='24-10-2016' or git log --since='24-10-2016'
--untilare synonym for
git log --date=<option>
--dateflag is used to format the output of the date. There are different option available for
--dateflag such as short, iso8601, relative, etc…
git log --author=<name>When you are only looking for commits by certain user you can
--authorflag. You can also use regular expressions for this.
git log --author='John\| Mary'
git log --grep='<message>'When you want to search for log with certain string using
--grepflag. You can use
-ioption if you want ignore the case.
git log -- something.rb incredible.rbWhen you want to get log only on some files you can specify files after
By ContentGit log allows you to get logs of files containing certain string. You can specify string using the
git log -S"Hello World!"
Format options available
git log --pretty=format:'<options>'Git log format options
%H - Commit hash %h - Abbreviated commit hash %T - Tree hashes %t - Abbreviated tree hash %P - Parent hashes %p - Abbreviated parent hashes %an - Author name %ae - Author email %ad - Author date (format respects the --date=option) %ar - Author date, relative %cn - Committer name %ce - Committer email %cd - Committer date %cr - Committer date, relative %s - Subject %B - MessageExample
git log --pretty=format:"%B on %ad with hash %h by %an" --date=shortThat was a bit on how we can use Git log, and specifically on how to format the output. Leave your comments, questions, and doubts in the comment section in case you need to reach me.]]>