Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A script to log commits from a Git hook
#!/usr/bin/env ruby
# Write git commit messages to a log file
# Lincoln A. Mullen | |
# MIT License <>
# You will have to install the git gem for this to work:
# gem install git
# Name this file 'post-commit' and drop it in the directory '.git/hooks' in
# any repository that you want to log. Make sure the file is executable. You
# can also add this to your git templates, which will put it in every new
# repository or to existing repositories by re-running git init.
# A commit message in the log should look this this:
# 2013-07-26-11-25-55 [repository:branch] First line of commit message
require "git"
log_file = "/home/lmullen/todo/time-use.txt"
repo =
repo_name = Dir.pwd[%r{[\.\-\w]+$}] # top directory
date ="%Y-%m-%d-%H-%M-%S") # format date
message = repo.log.first.message.lines.first.strip # just first line
branch =, "a") do |log|
log.puts "#{date} [#{repo_name}:#{branch}] #{message}"

This comment has been minimized.

Copy link

@andrewmartin andrewmartin commented Aug 21, 2013

Does this work for you still? I'm having some issues getting it to work. This is super cool, would love to get it implemented if you have a second.


This comment has been minimized.

Copy link

@mmorey mmorey commented Dec 25, 2013

Works for me. But make sure you make the file executable.

chmod +x post-commit

You can also put some puts in the script to test if it is working. See my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment