Skip to content

Instantly share code, notes, and snippets.

@pocke
Last active January 25, 2024 07:27
Show Gist options
  • Save pocke/c54be87893aefa2be76abe1b2a4cdca5 to your computer and use it in GitHub Desktop.
Save pocke/c54be87893aefa2be76abe1b2a4cdca5 to your computer and use it in GitHub Desktop.
Add GitHub's Co-authored-by to the latest commit from account name
#!/usr/bin/env bash
USAGE="Usage: git coauthor USERNAME
It adds the specified user as Co-authored-by into the latest commit.
Example:
$ git coauthor pocke"
if [ -z $1 ]; then
echo $USAGE
exit 1
fi
api_resp=$(gh api "/users/$1")
id=$(echo "$api_resp" | jq .id)
name=$(echo "$api_resp" | jq .name)
email=$(echo "$api_resp" | jq .email)
if [ -z $email ]; then
email="$(echo $id)+$(echo $1)@users.noreply.github.com"
fi
email=$(echo $email | tr -d '"')
title=$(git show -s --format=%s)
body=$(git show -s --format=%b)
git commit --amend -m "$title" -m "$body" -m "Co-authored-by: $name <$email>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment