Give credit where credit's due

I love pair programming. It is a great way to improve solutions, share context, and continuously review code. But the passenger in a pairing session often ends up being the unsung hero, not showing up in the commit history for their invaluable contributions because they were not typing on the keyboard.

Commit logs are obviously not the authority on an individual’s contributions to a codebase, but I still like to give credit where credit is due. Fortunately, using git’s trailers, Github allows you to commit together. Including a Co-authored-by: citation with your pairing partner’s name and email will properly attribute them in Github’s commit log.

I have used this to share credit for several years, but it is a bit painful. Making sure to type your partner’s name and email is a hassle. Any typo will not credit appropriately and become permanantly engraved in the history of your repository when you merge it to master!

So avoid the typos!

First, I created a simple git alias that will allow me to easily modify a commit to add a trailer. In your global .gitconfig file, add

  add-trailer = commit --no-edit --amend --trailer

This can be used as part of a two step process:

  1. Make your commit as you normally would
  2. Use the alias to add a trailer
  • git add-trailer "Co-authored-by: Partner <a@example.com>"

We can still have typos though. But if our pairing partner has worked on this repository in the past, we can find their information in the git log. This is still painful to do manually. Fzf can help us here though. We can pass our log information into fzf and then pass our selection on to our newly created git alias:

git log --pretty="%an <%ae>" \
    | sort -u \
    | fzf \
    | xargs -I "{}" git add-trailer "Co-authored-by: {}"

Breaking this down:

  • git log --pretty="%an <%ae>"
    • List the author of every commit in our repositry and format it appropriately for attribution
  • sort -u
    • Make the list sorted and unique
  • fzf
    • Prompt us to make a selection from the list
  • xargs -I "{}" git add-trailer "Co-authored-by: {}"
    • Take the result from our prompt and pass it into our new git alias

And when everything is in place, attribution is a breeze!

Example git acknowledge

And in the spirit of attribution, thanks to last week’s pairing partner:

Encouraged-by: nichol alexander <nichol.alexander@gmail.com>