Suppose you’re doing some pair-programming with a co-worker. That means only one of you can commit the work you’re both doing.
But you can credit the other party by using a Git feature called Co-authored-by
. This will show up on GitHub (and probably other clients as well). See GitHubs documentation on how it will look like.
When writing the commit message, add an empty line and then Co-authored-by: Name <email>
, for example:
Initial commit
Co-authored-by: Arne Bahlo <[email protected]>
If you want to credit multiple people, put them on their own line, each starting with Co-authored-by:
.
David Nguyen has built a ZSH script called fcm, which takes a .git-co-authors
file and lets you interactively add an author to the commit.
I adapted it to take the authors from the current repository and did some small improvements:
#!/bin/zsh
SELECTED_AUTHORS=$(git shortlog -sne | awk '{$1=""}1' | fzf -m)
MESSAGE="\n\n"
# convert newline-delimited string to array, zsh way: https://stackoverflow.com/a/2930519
AUTHORS=("${(f)SELECTED_AUTHORS}")
for AUTHOR in $AUTHORS[@]; do
MESSAGE="${MESSAGE}Co-authored-by: ${AUTHOR}\n"
done
if [[ "$1" == "-m" ]]; then
git commit -m "$2$(echo -e ${MESSAGE})"
else
git commit $@ -t <(echo -e ${MESSAGE})
fi
Copy this to a file called git-co-author-commit
in a directory called zshfunctions
, then load it like this in your ~~/.zshrc~:
fpath+=/path/to/zshfunctions
autoload git-commit-co-author
You’ll also need FZF for the fuzzy matching.
You can then run git-commit-co-author -v
(or similar) and it will prompt you for a person in the repository, then open the commit message with Co-authored-by
already added. I recommend creating a shorter alias for it.
Note that when running this with -m
, make sure it’s the first and only parameter (followed by the message of course).