I prefer to use Github Desktop than cli when developing code. What can i say, i’m a UI guy! Our Github organisation requires a Developer Certificate of Origin (DCO) on Pull Requests. This essentially means you must sign every commit. It can be easy to forget to do this (and a PITA the remediate that usually involves asking my buddy Ryan Johnson to help with 🙂 ) so I wanted to find a way of auto-signing commits when using Github Desktop. The instructions below are for Mac but should be adaptable to Windows.
First, install GPG-Suite to enable creation of a GPG key.
brew install --cask gpg-suite
#!/bin/sh
SOB="Signed-off-by: $(git config user.name) <$(git config user.email)>"
if ! grep -q "$SOB" "$1"; then
echo "$SOB" >> "$1"
fi
chmod +x .git/hooks/prepare-commit-msg
To enable for a single repo
# From your repo root:
mkdir -p .git/hooks
cat > .git/hooks/prepare-commit-msg <<'SH'
#!/bin/sh
# Auto-append DCO "Signed-off-by" to commit messages.
MSGFILE="$1"
SOURCE="$2" # e.g., message|template|merge|squash|commit|revert
SHA="$3"
# Skip merge/revert commits (usually generated by Git)
case "$SOURCE" in
merge|revert) exit 0 ;;
esac
NAME="$(git config user.name)"
EMAIL="$(git config user.email)"
# If identity is missing, don't modify the message
[ -z "$NAME" ] && exit 0
[ -z "$EMAIL" ] && exit 0
SOB="Signed-off-by: $NAME <$EMAIL>"
# If our exact Signed-off-by is already present, do nothing
if grep -qiE "^Signed-off-by:\s*$(printf '%s' "$NAME" | sed 's/[].[^$*\/]/\\&/g')\s*<$(printf '%s' "$EMAIL" | sed 's/[].[^$*\/]/\\&/g')>" "$MSGFILE"; then
exit 0
fi
# Ensure there is a trailing newline before appending
# (portable check: if last byte isn't newline, add one)
lastchar="$(tail -c 1 "$MSGFILE" 2>/dev/null)"
[ "$lastchar" = "" ] || echo "" >> "$MSGFILE"
# Append our Signed-off-by line
echo "$SOB" >> "$MSGFILE"
SH
chmod +x .git/hooks/prepare-commit-msg
To enable for all repos
mkdir -p ~/.git-hooks
cp .git/hooks/prepare-commit-msg ~/.git-hooks/prepare-commit-msg # or create it directly there
chmod +x ~/.git-hooks/prepare-commit-msg
git config --global core.hooksPath ~/.git-hooks
Happy coding!