Using Git Log to Show Last Month's Commits

- jgilman - Rails

Every month, I have to produce a short bulleted list of tasks I worked on from the previous month. Usually I end up straining my memory trying to remember everything I did. The last time I had to make this list I realized that our git repository contained all this information. I had to find a good way to extract it. git log is the right tool when you need to explore commit history.

Showing My Commits

The first thing I needed was to only show commits that I had made and ignore commits from author developers. The --author argument allows you to specify a pattern or string to match against commit authors. git log --author="Jason Gilman" will show just my commits. I wanted a git command that I could share with other developers so I didn’t want to embed my username in it. Your git author name comes from the .gitconfig file in your home directory. It can be retrieved with the command git config

I’m also only interested in the commit date, hash, and message. The --pretty argument can be used to limit the output of log. Passing --pretty=format:"%cd %h %s" along with --date=short to show only the day would show just the information I wanted.

The command I had produced so far was this

<code>git log --author="`git config`" --pretty=format:"%cd  %h  %s" --date=short
> sample output
2013-04-18  e3feb22  Undoing frim fram adjustments
2013-04-07  0a3a1df  Reticulating mumbo jumbo
2013-04-05  7b4bf01  Correcting mispelling
2013-04-05  206ed78  Harnessing power of the force

Showing Last Month’s Commits

Now that I had just my commits in a decent format I needed to find last month’s commits. There are a bunch of different ways to search using git log. One useful argument is --since. You can pass it simple phrases like git log --since="last month". This looks promising but wasn’t what I wanted. If today was April 18th it would show me all the commits from March 18th until today. I wanted all of the commits in March. The solution turned out to be the use of --before and --after which accept dates in the format of YYYY-MM-DD. I can show all of my commits for the month of March with this command.

<code>git log --before={2013-04-01} --after={2013-03-01} --author="`git config`" --pretty=format:"%cd  %h  %s" --date=short

I wanted to be able to run this command every month without having to manually type in a new date. The date command can be used to retrieve dates in different formats. It takes different arguments depending on the OS. The current date can be printed on Mac OSX with date "+%Y-%m" and the previous month can be printed with date -v-1m "+%Y-%m" If we substitute those two in place of the dates we have a command that will show all of my commits from the last month. I also appended --reverse so that the commits would be shown in ascending order.

<code>git log --before={`date "+%Y-%m-01"`} --after={`date -v-1m "+%Y-%m-01"`} --author="`git config`" --reverse --pretty=format:"%cd  %h  %s" --date=short

Putting it in Git Config

I don’t want to have to remember that full command so I created an alias for it in ~/.gitconfig.

<code># inside ~/.gitconfig
  # other aliases here
  my-commits-last-month = !git log --author=\"`git config`\" --before={`date "+%Y-%m-01"`} --after={`date -v-1m "+%Y-%m-01"`} --reverse --pretty=format:\"%cd  %h  %s\" --date=short

Now I can show my commits from the previous month just by running git my-commits-last-month