Git Protip: Learning from your history (git log) 02 Dec 2008
slide software development git

I've been sending weekly "Protip" emails about Git to the rest of engineering here at Slide for a while now, using the "Protips" as a means of introducing more interesting and complex features Git offers. Below is the second Protip written to date.

One of the major benefits to using Git is the entirety of the repository being entirely local and easily searched/queried. For this, Git has a very useful command called git log which allows you to inspect revision histories in numerous different ways between file paths, branches, etc. There are a couple basic scenarios where git log has become invaluable, for me at least, in order to properly review code but also to track changes effectively from point A to point B.

  • What's Dave been working on lately? (with diffs)
    • git log -p --no-merges --author=dave

    The --no-merges option will prevent git log from displaying merge commits which are automatically generated whenever you pull from one Git branch to another

  • Before I merge this branch down to my team master, I want to know what files have been changed and what revisions
    • git log --name-status master-topfriends...proj-topfriends-thing

    Git supports the ability with git log and with git diff to provide unidirectional branch lookups or bidirectional branch lookups. For example, say the left branch has commits "A, B" and the right branch has commits "A, C". The ... syntax will output "C", whereas .. will output "B, C"

  • I just got back from a vacation, I wonder what's changed?
    • git log --since="2 weeks ago" --name-status -- templates

    At the tail end of a git log command you can specify particular paths to look up the histories for with the -- operator, in this case, I will be looking at the changes that have occured in the templates directory over the past two weeks

  • Most recent X number of commits? (with diffs)

    • git log -n 10 --no-merges -p

All git log commands automatically filter into less(1) so you can page through the output like you would normally if you executed a svn log | less. Because git log is simply reading from the locally stored revision history you can quickly grep the history by any number of different search criteria to gain a better understanding of how the code base is changing and where.

For more specific usage of `git log` refer to the git log man page

Did you know! Slide is hiring! Looking for talented engineers to write some good Python and/or JavaScript, feel free to contact me at tyler[at]slide