Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Place this in the following path relative to your git repo's root: .git/hooks/commit-msg
#!/usr/bin/env ruby
# Hook into the message and append the branch name so that we don't have to
# manually do it ourselves!
# This is the message that you put when you do:
# `git commit -m "This is my message"
message_file = ARGV[0]
message =
branch_name = `git rev-parse --abbrev-ref HEAD`.strip
# We don't want to do this if we're on master, that'd be weird!
if branch_name != "master"
# Checks if the branch name is already in the commit message
unless message.include?branch_name
prepend = "[#{branch_name}] "
message = "#{prepend} #{message}"
puts message
# Rewrite the commit message with the branch name in front., "w") { |f|
f.write message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.