Making Sense of Git Log files
Git logs allow you to review and read a history of everything that happened to a repository. The history is built using git-log, a simple tool with a ton of options for displaying revision history.
What is a Git Log? #
A Git log is a running record of commits. A full log has the following pieces:
- A commit hash (SHA1 40 character checksum of the commit contents).
- Commit Author metadata: The name and email address of the author of the commit.
- Commit Date metadata: A date timestamp for the time of the commit
- Commit title/message: The overview of the commit as written in the commit message.
A Typical Git Log #
Git logs can be whatever you want them to be. git-log
offers dozens and dozens of options but let’s start with the simplest.
$ git log
This outputs the most basic log:
commit 98aa8d722bdecc4e56156cfe1a793a4d16848eb8
Author: Ryan Irelan <[email protected]>
Date: Sat Jan 10 23:26:40 2015 -0600
Adding in new homepage
Includes the assets needed for Foundation
commit dd8d6f587fa24327d5f5afd6fa8c3e604189c8d4
Author: Ryan Irelan <[email protected]>
Date: Tue Jan 6 20:07:17 2015 -0600
added origination declaration at bottom of RSS feed
This is a snippet of the log, showing two commits. We have a commit SHA1 hash, the author, the date, and the commit message, explaining what happened in the commit.
This layout is the default look of the log. Git has something called Commit Limiting to make it easier to narrow down hundreds or thousands of commits to the ones you want to review.
Directory Restricted Log #
The default log is great for grabbing a quick look at what just happened in the repository. But it takes up a lot space and you can only see a handful of commits at once.
When I’m developing a project, I sometimes only want to know what happened in a specific directory. Let’s say I’m working on some CSS or Sass for a web-based project and only want to know about changes in my Sass directory. I can get much more specific with git-log and restrict it only to a specific directory.
git log scss
This will only return commits that had changes in the scss
directory.
Log by branch #
We can use a similar syntax as directory restriction and build a log for just one branch. We only need to specify the branch we want to see.
git log develop
We can clean that up a little by removing any merge commits (which can bulk up the log if there are a lot of merges, like there would be a develop
branch.
git log develop --no-merges
On larger projects, with multiple developers working and committing changes, the log can get unwieldy very quickly. We just learned how to restrict by directory, but let’s restrict by time.
Time-restricted log #
Nothing beats a sense of accomplishment at the end of the day like looking back at everything you did. Git-log let’s us review our work for the day by restricting to just commits we made today.
$ git log --since=8am
This will output all of the commits made by everyone since 8 AM this morning. That’s great for reviewing what the entire team has accomplished but I’d like to see what I have accomplished. Let’s make it a bit more specific.
git log --since=8am --author=ryan
Ah, that’s much better!
In the morning, before my coffee really starts to kick in, I like to review the work I did the previous day on a specific project. For this we can use a modified version of the last git log
command to show only what we worked on yesterday.
git log --since=yesterday --author=ryan
That works if we haven’t made any commits yet today.
To review a project’s work for the entire team over the course of a month, we can use --before
and --after
again and just leave off the the --author
flag.
$ git log --before={2015-01-01} --after={2014-11-30}
Now we’re building the log using only commits that were created after November 30, 2014 and before January 1, 2015. That isolates the time period to only the month of December.
We can use the --before
and --after
flags to handle pretty much any time period we want, which makes this a powerful way to build a commit log that works for whatever we need.
Searching Commits to Build a Log #
We can also limit the log that is outputted by search terms. To do this we use the --grep
option and pass in a term or regular expression.
$ git log --grep="homepage"
This will return a log with commits that only reference the word “homepage” in the commit message.
Seeing Diffs in the Log #
When we get our log output very specific we may want to expand the amount of information that we’re showing there so we the commits we’re viewing are as helpful as possible.
We do that using the -p
option to show diffs of the changed files.
git log -p
If we keep our commits as atomic as possible then this diff view should be fairly easy to manage.
Let’s put together everything we’ve learned up to this point with specifying how our log outputs.
$ git log -p --grep="homepage" --before={2015-01-01} --after={2014-11-30}
--author=ryan
This git log
command shows the full diff (the -p
), searched the commit messages for the word “homepage”, only uses commits in the month of December 2014, and they’re only authored by me.
Git Log with Graph #
I like to see a quick glance at the git log
that shows me the structural history of the repository. I want to see where branching and merging happened.
Git offers a simple way: the --graph
option.
$ git log --graph
This will draw a text-based graph of the commit history. Along the left side of the output you should see a series of dashes. If you have branches and merges in the history of your repository then you should see those indicated.
One note: if you’re running the command in a small terminal window, you might want to expand it to see the graph appear properly.