Validate that all commits in the current PR have Co-Authored-By trailer. Rewrites missing ones via interactive rebase.
Use
/co-authoredto ensure all commits in the current PR have the Co-Authored-By trailer.
Detect the PR from the current branch:
gh pr view --json number,baseRefName,headRefName,commits
If no PR is found, inform the user and stop.
MY_EMAIL=$(git config user.email)
MY_LOGIN=$(gh api user --jq '.login')
Get all commits between the base branch and HEAD:
git log origin/<base>..HEAD --format="%H %ae %s"
Only process commits authored by the current user. Match by email ($MY_EMAIL) or by GitHub username ($MY_LOGIN). Skip all other commits — they belong to other contributors and must not be touched.
For each of my commits, inspect the full message:
git log -1 --format="%B" <sha>
Check if it contains Co-Authored-By: Claude <[email protected]>.
Build two lists:
If all my commits are OK, inform the user and stop.
## Commits missing Co-Authored-By
| # | SHA | Message |
|---|-----|---------|
| 1 | abc1234 | feat: add login page |
| 2 | def5678 | fix: handle null case |
These commits will be rewritten to add the trailer. This requires a force push.
Proceed?
Wait for user confirmation before proceeding.
Use git rebase with GIT_SEQUENCE_EDITOR to automate the rebase. For each commit that needs the trailer, change pick to reword:
GIT_SEQUENCE_EDITOR="sed -i 's/^pick <short_sha>/reword <short_sha>/'" \
git rebase -i origin/<base>
Then for each reword step, use GIT_EDITOR to append the trailer to the commit message. Use an env-based editor script:
# Create a temporary script that appends the trailer if missing
cat > /tmp/add-coauthor.sh << 'SCRIPT'
#!/bin/bash
if ! grep -q "Co-Authored-By: Claude" "$1"; then
echo "" >> "$1"
echo "Co-Authored-By: Claude <[email protected]>" >> "$1"
fi
SCRIPT
chmod +x /tmp/add-coauthor.sh
GIT_SEQUENCE_EDITOR="sed -i -E 's/^pick (SHORT_SHAS_PIPE_SEPARATED)/reword \1/'" \
GIT_EDITOR="/tmp/add-coauthor.sh" \
git rebase -i origin/<base>
Replace SHORT_SHAS_PIPE_SEPARATED with the short SHAs of missing commits joined by | for the sed regex.
git push --force-with-lease
git log origin/<base>..HEAD --format="%H %s"
Check all commits now have the trailer and report:
## Done
All N commits now have Co-Authored-By trailer.
Force pushed to origin/<branch>.
--force-with-lease instead of --forcegit rebase --abort and inform the user