Skip to content

Instantly share code, notes, and snippets.

@jasonmadigan
Created July 22, 2024 09:24
Show Gist options
  • Save jasonmadigan/a795db5ac95cf16697a469cce9eaf25a to your computer and use it in GitHub Desktop.
Save jasonmadigan/a795db5ac95cf16697a469cce9eaf25a to your computer and use it in GitHub Desktop.

DCO

git commit templates only work with interactive message writing. Need a hook for -m commits.

Create a hook

cd .git/hooks
touch commit-msg
chmod +x commit-msg

commit-msg:

#!/bin/sh

# Get the commit message file
COMMIT_MSG_FILE=$1

# Check if the commit message already contains a "Signed-off-by" line
if ! grep -q "^Signed-off-by:" "$COMMIT_MSG_FILE"; then
    # Append the "Signed-off-by" line to the commit message
    echo "\nSigned-off-by: $(git config user.name) <$(git config user.email)>" >> "$COMMIT_MSG_FILE"
fi

Test

echo "Test with commit-msg hook" > test.txt
git add test.txt
git commit -m "Test commit using -m flag"
git log --pretty=full
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment