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 Graphs
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.
Custom formatting
git log --pretty=format:"<string>"This lets you display each commit however you want in printf style. For example
%cn
, %h
and %cd
which represents committer name, abbreviated hash and committer date respectively.
git log --pretty=format:"%cn committed %h on %cd"
Filtering the output
Git log can be filtered by different filters and format outputBy amount
git log -<n>It displays latest n commits and its output. For example
git log -3Will display latest 3 commit details
By date
git log --after=<date> git log --before=<date>Using
--after
and --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'
--since
and and --until
are synonym for --after
and --before
git log --date=<option>
--date
flag is used to format the output of the date. There are different option available for --date
flag such as short, iso8601, relative, etc…
By Author
git log --author=<name>When you are only looking for commits by certain user you can
--author
flag. You can also use regular expressions for this.
git log --author='John\| Mary'
By message
git log --grep='<message>'When you want to search for log with certain string using
--grep
flag. You can use -i
option if you want ignore the case.
By file
git log -- something.rb incredible.rbWhen you want to get log only on some files you can specify files after
--
flag
By Content
Git log allows you to get logs of files containing certain string. You can specify string using the-S
flag
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.]]>