GIT Co-Author Helper Script
Phil Hadviger
Principal Site Reliability Engineer @ GLG#
GIT Co-AuthorsGitHub and GitLab both support the idea of co-authors which allow a user to attribute a commit to more than one author by adding one or more Co-authored-by
trailers to the commit's message.
Co-author attibuting looks somethink like the following in the commit message.
So at the time of the commit, it requires that the author has the proper email address of the user they are trying to add as a co-author.
When this commit is pushed to the GitHub/GitLab repo, you'll see the co-authored commit showing up, looking something like this.
Here are some things to note about the picture above.
You can see the two author icons at the top, saying that the commit was done by multiple users.
The commit is also marked partially verified. This is due to:
The primary author having GIT setup to sign his commits using his own GPG private key and GitHub confirming his signed commit.
The secondary author also has GitHub setup to require his commits to be signed and verified
The primary author does not have access to the secondary author's GPG private key, so he's only able to attribute the commit, but not have GitHub verify it.
#
Methods for adding Co-Authors#
Manually add it to the commitThe most error prone and labor intensive version of this. This requires the user to have the email address of the secondary author, and would mean that in every commit, he has to enter the proper co-author attribution line. eg. Co-authored-by: name <name@example.com>
I've tried this way a long time ago, and that was just to painful.
commit.template
Setting#
Use the GIT has a config setting called commit.template
that can be configured to point at a template file. This would be set something like this.
And the .gitmessage
file would contain:
Now every time the user commits, the contents of the .gitmessage
file would be automatically populated into the message of the commit. The user can then add new co-authors by editing the template or remove co-authors by deleting lines. Easier than the method before, but still wasn't really ideal for me.
#
Automate Co-AuthorsI wanted to automate the options above to accomplish a few things.
- I like
commit.template
files, so I wanted to preserve that in my workflow - I wanted whatever option to come up with still support all the
git commit
arguments - I didn't want to manage the email addresses and user names myself
- Co-Authors need to be relevant to what I'm working on, even in something like a giant infrastructure monorepo.
I'll explain what I came up with in the comments are more below.
When I run this script, I get a popup looking something like this:
This is a test repo, so not the greatest example, but basically a list shows up and I use TAB to select co-authors, and they get automatically inserted into the git commit message.
#
Some more Info about the scriptWhenever I was to commit something with co-author attribution, I now use the function.
#
Notes- I've tested this on OSX Catalina and Ubuntu 18.04
commit.template
supports~/
abbreviations for the path, but this script currently requires a full or relative path to thecommit.template
file.- My actual script has
--gpg-sign
added to thegit commit
inside the script, so I didn't include it in this script, since I figured it was sort of a customization not needed for the example. - I have my version in a public GitHub repo, so if you find bugs or want to give suggestions, please open an issue there.