0

When I run git commit, my favourite editor starts and presents me with something like this:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
# …

I work on many different projects in different context, and as such have various Git identities. More than once I have to change an existing commit (or worse, a chain of existing commits) because I forgot to run git config user.email.

Is it possible to show the current identity in the comments of the commit message, so that I see it when I write my commit message? That would help me to discover mistakes earlier.

Something along the lines of this would be fine:

# Author:
# jornane <jornane@example.com>
jornane
  • 1,085
  • 6
  • 14

1 Answers1

0

You didn't specify what OS you are using but on Linux you can achieve what you want using the following prepare-commit-msg:

#!/usr/bin/env sh

# prepare-commit-msg: print author name and e-mail as a comment in a commit message
#                     automatically

# if amend, don't do anything
if ! [ -z $3 ] ;then
    exit
fi


author=$(git var GIT_AUTHOR_IDENT | grep -E -o ".*<.+>")

sed -i "1s/^/#$author \n/" $1
Arkadiusz Drabczyk
  • 2,624
  • 1
  • 14
  • 13
  • From your answer, I found http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#Client-Side-Hooks, but that only applies to per-repo hooks. Since I forget to set the author, I would also forget to set hooks. http://stackoverflow.com/questions/2293498/git-commit-hooks-global-settings seems to have more information on the matter, I'll look into that. – jornane Jul 24 '15 at 09:04