Git log – Quick Reference

Some useful git log and git show options which I am bound to forget and will be happy to have in a singe place!

Limit the log entries

$ git log -<number of entries>
ex: git log -2 shows the last two commit details
$ git log -2
commit 474ea8e80dc30576b8b8f764a4f52c72e54277bd
Author: Nikhil Kuriakose <nikhilkuria@hotmail.com>
Date: Tue Sep 16 20:38:39 2014 +0530

draft v0.1 changes added

commit 3ac4e6fae7098951746dc46947bd12baec4b1f22
Author: nikhilkuria <nikhilkuria@hotmail.com>
Date: Fri Aug 29 09:55:53 2014 +0530

Bug fix : Some nodes were having trouble finding their parent.. phew!!

OK, so how about if we want to look at all the commits made since yesterday? All we need is the --since option:

git log --since="yesterday"

We can also use --until to fetch all commits up to a certain date; or of course we can use both options in tandem:

git log --since="1 week ago" --until="yesterday"

There is also a neat way to search the commits. Suppose you are working on a method and you need to know the history of changes made on it. Say, I am interested in the method, updateMetaDataMaps

git log -SupdateMetaDataMaps

This shows the commits that introduced a change to the code that added or removed that string.

Neat formats to the log

A simple git log statement gives us so much information that i need at the moment.

$ git log
commit 474ea8e80dc30576b8b8f764a4f52c72e54277bd
Author: Nikhil Kuriakose <nikhilkuria@hotmail.com>
Date: Tue Sep 16 20:38:39 2014 +0530

draft v0.1 changes added

commit 3ac4e6fae7098951746dc46947bd12baec4b1f22
Author: nikhilkuria <nikhilkuria@hotmail.com>
Date: Fri Aug 29 09:55:53 2014 +0530

Bug fix : Some nodes were having trouble finding their parent.. phew!!

commit 0051792ee0808e781a17d95cfb0d7b56a4402335
Author: = <=>
Date: Wed Aug 27 18:07:28 2014 +0530

Sensible to split the attributes

commit 1da57b8e21ace5238a6303920f52cf7363882067
Author: ueec6tv <extern_kuriakose.nikhil@allianz.com>
Date: Tue Aug 19 17:20:12 2014 +0530

extra lines!!!

And if you think you need even more information, the file changes can be shown using

$ git log -p

But, if you just need to see a minimal view, the –pretty=oneline option will help,

$ git log --pretty=oneline
474ea8e80dc30576b8b8f764a4f52c72e54277bd draft v0.1 changes added
3ac4e6fae7098951746dc46947bd12baec4b1f22 Bug fix : Some nodes were having trouble finding their parent.. phew!!
0051792ee0808e781a17d95cfb0d7b56a4402335 Sensible to split the attributes
1da57b8e21ace5238a6303920f52cf7363882067 extra lines!!!
eeb5c9a33462c2bfb6c058b55bbf6cb97d799607 Adding a sample xml file

–pretty also comes with options short, full and fuller options to show the output in roughly the same format but with less or more information, respectively

$ git log --pretty=short
commit 474ea8e80dc30576b8b8f764a4f52c72e54277bd
Author: Nikhil Kuriakose <nikhilkuria@hotmail.com>

draft v0.1 changes added

commit 3ac4e6fae7098951746dc46947bd12baec4b1f22
Author: nikhilkuria <nikhilkuria@hotmail.com>

Bug fix : Some nodes were having trouble finding their parent.. phew!!
$ git log --pretty=fuller
commit 474ea8e80dc30576b8b8f764a4f52c72e54277bd
Author: Nikhil Kuriakose <nikhilkuria@hotmail.com>
AuthorDate: Tue Sep 16 20:38:39 2014 +0530
Commit: Nikhil Kuriakose <nikhilkuria@hotmail.com>
CommitDate: Tue Sep 16 20:38:39 2014 +0530

draft v0.1 changes added

commit 3ac4e6fae7098951746dc46947bd12baec4b1f22
Author: nikhilkuria <nikhilkuria@hotmail.com>
AuthorDate: Fri Aug 29 09:55:53 2014 +0530
Commit: nikhilkuria <nikhilkuria@hotmail.com>
CommitDate: Fri Aug 29 09:55:53 2014 +0530

Bug fix : Some nodes were having trouble finding their parent.. phew!!

One of the most frequent things I have to do is find a commit based on a comment and see the changes. For example, I need to see the changes I made for a defect XYZ. I dont remember when I made the change, but I am sure, I will have the defect ID in the comment.

This is where the –pretty=oneline comes in handy, because the comment and the hash will be in one line

$ git log --pretty=oneline --grep="5716"
3546c34211dfa2de1477ee8a1c12d075ccc3729b fix for td 5716. adding extra validation

now, if i get the hash, i can quickly do a git show to see the changes i made,

$ git show 3546c34211dfa2de1477ee8a1c12d075ccc3729b