40

My client git version is 1.7.0.4.

I want to automatically add a "Signed-off-by" line for the committer at the end of the commit log message when commit a message.

  1. When I set git config --global format.signoff true, and run git commit -m "modify something", I see no "Signed-off-by" in git log.
  2. If I use git commit -m -s "modify something", then "Signed-off-by" does show in git log.

Can anyone help?

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
Venus
  • 1,184
  • 2
  • 13
  • 32

4 Answers4

45

There is now an easy way to automatically sign-off any commit that is not already signed-off by using hooks and the git-interpret-trailers command. In the upcoming version 2.15 of git the command allows to trivially check for an existing sign-off (no matter what's its value/author is) and add yours if there is non yet. As of October 2017 the required code is not in any git release yet (but in its master branch)!

Save the following as .git/hooks/prepare-commit-msg or .git/hooks/commit-msg (see here for the differences) and make it executable.

#!/bin/sh

NAME=$(git config user.name)
EMAIL=$(git config user.email)

if [ -z "$NAME" ]; then
    echo "empty git config user.name"
    exit 1
fi

if [ -z "$EMAIL" ]; then
    echo "empty git config user.email"
    exit 1
fi

git interpret-trailers --if-exists doNothing --trailer \
    "Signed-off-by: $NAME <$EMAIL>" \
    --in-place "$1"
stefanct
  • 2,503
  • 1
  • 28
  • 32
  • 3
    Nicely done. +1. I reference your answer in mine for more visibility, and reference https://stackoverflow.com/a/41361273/6309 where I mentioned `git interpret-trailers`. – VonC Oct 08 '17 at 06:45
  • Thanks for proposing a solution based on this `git-interpret-trailers` technique. To me, the decisive advantage is that this works regardless of the tool triggering the commit and how it's configured -- I often do that with `git gui`. – Dr. Jan-Philip Gehrcke Oct 28 '20 at 09:32
  • Unfortunately, this is not always the case. I can't remember exactly which but I had some application that worked around this (one can do similarly with `git commit -n`) and committed directly w/o executing the hook. – stefanct Oct 29 '20 at 10:17
  • 3
    If you are on a newer version of git (I am on 2.25.x), `/.git/hooks/prepare-commit-msg.sample` comes with the code for sign-off (it's commented). You need to `cp prepare-commit-msg.sample prepare-commit-msg` and uncomment the line that starts with `SOB=$(git var GIT_AUTHOR_IDENT...` and the line after that. Make the file executable using `chmod +x /.git/hooks/prepare-commit-msg` and you are good to go. – vadasambar Aug 16 '22 at 05:49
24

You can signoff by default in a project by creating a file (say "~/MYPROJECT/git-template") that contains some blank lines and the signed-off-by text like this:

Signed-off-by: Your Name <your.email@example.com>

Then configure git to use that as a commit template. For example:

git config commit.template ~/MYPROJECT/git-template

Make sure your project documents somewhere what signed-off-by means for the project. Here's some text you can copy into your CONTRIBUTING.md file:

All contributions (including pull requests) must agree to the Developer Certificate of Origin (DCO) version 1.1. This is exactly the same one created and used by the Linux kernel developers and posted on http://developercertificate.org/. This is a developer's certification that he or she has the right to submit the patch for inclusion into the project. Simply submitting a contribution implies this agreement, however, please include a "Signed-off-by" tag in every patch (this tag is a conventional way to confirm that you agree to the DCO).

hurikhan77
  • 5,881
  • 3
  • 32
  • 47
user45404
  • 349
  • 2
  • 2
16

Update for Git 2.14.x/2.15: as I mentioned in "Git - Detect if commit is signed off programmatically", you will be able to parse a commit message trailer for Signed-off-By line.

"git interpret-trailers" has been taught a "--parse" and a few other options to make it easier for scripts to grab existing trailer lines from a commit log message.

See stefanct's answer for commit-msg client-side hook which would use git interpret-trailers.


Original answer (2013)

format.signoff is about patch (as illustrated, for instance, in this patch):

format.signoff

A boolean value which lets you enable the -s/--signoff option of format-patch by default

It has no bearing on git commit -s.

In other words, you don't have to signoff every commit, but when you are publishing them as patch for others to use (as in "git: submitting patches"), then you should sign them.

For the exact signification of Signed-of-by, see "What is the Sign Off feature in Git for?".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

To automatically add the --signoff/-s option to all commits you make in a git repository:

cd <REPO>
cp .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg
edit .git/hooks/prepare-commit-msg

Then uncomment the last block of code:

SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE"
if test -z "$COMMIT_SOURCE"
  then
    /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE"
fi
Elijah Lynn
  • 12,272
  • 10
  • 61
  • 91