Skip to content

Instantly share code, notes, and snippets.

@hsbt
Forked from pocke/git-coauthor
Last active January 25, 2024 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsbt/897d6fc1c8a2721c60cb767bf231bd8b to your computer and use it in GitHub Desktop.
Save hsbt/897d6fc1c8a2721c60cb767bf231bd8b 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