DEV Community

Cover image for Co-authoring Git commits
Cassidy Williams
Cassidy Williams

Posted on • Edited on • Originally published at cassidoo.co

Co-authoring Git commits

Sometimes when I'm pair programming (or if someone's been just super helpful to me), I want to be able to give another developer credit in a commit I make in a Git repo.

There's a way to do this (that is supported in both GitHub and GitLab) from the command line! In your commit, you just have to add this to your commit message:



Co-authored-by: name <someemail@example.com>


Enter fullscreen mode Exit fullscreen mode

Now, if you don't know that person's email address configured in Git, I personally just run git log and then scroll through until I find it (which works well on small teams or when that person has a branch that you can checkout and run this on). You could also get a bit more fancy and run git show <some commit ID where you know they made the commit> --format=email to get just their email. Or you can be less fancy and just ask them.

This is fairly easy to do in the various Git clients out there since there's a nice open text box for it, but in case you like to use the command line, this is how you'd write it:



git commit -m "Regular commit message" -m "Co-authored-by: name <someemail@example.com>"


Enter fullscreen mode Exit fullscreen mode

And then, once you push, GitHub (or GitLab or wherever you host your Git projects that have this enabled) will credit that person with the commit alongside you!

Cassidy and Grady co-committed

Happy coding!

Top comments (12)

Collapse
 
valscion profile image
Vesa Laakso

This is neat! I am myself using the fancy --trailer flag that git commit also provides to do the same:

git commit -m "The commit message" --trailer 'Co-authored-by: name <person@example.com>'
Enter fullscreen mode Exit fullscreen mode

I have even saved this to a git alias when I'm pair-programming more often so that I get the trailer appended automatically. For example:

git config --global alias.commit-jane "commit --trailer 'Co-authored-by: Jane Doe <jane.doe@example.com>'"
Enter fullscreen mode Exit fullscreen mode

Now git commit-jane allows me to commit with Jane in Co-authored-by :)

Collapse
 
oanaom profile image
Oana Munteanu

Ah this is so cool! I'm totally saving this in my gist files. thanks for sharing this!

Collapse
 
vjnvisakh profile image
Visakh Vijayan

nice. Appreicating is fulfilling

Collapse
 
samarjaffri profile image
Samar F. Jaffri

Woa! This would be a great way of showing them that their time and effort is valued.
Thanks for sharing Cassidy!

Collapse
 
chiragagg5k profile image
Chirag Aggarwal

Github did this automatically when you commit a suggestion directly. Never know how to do this in command line. Really informative post ⭐️

Collapse
 
albarin profile image
Alba Rincón • Edited

Nice! 👏
I wrote a similar one some time ago, but to use it alongside git mob, which is a cool tool for pairing!
dev.to/albarin/how-to-easily-add-c...

Collapse
 
oanaom profile image
Oana Munteanu

ah I feel so bad that I didn't know about this until now and so happy that now I know this. Thanks for sharing this Cassidy! <3

Collapse
 
gradysalzman profile image
Grady Salzman

This is really great, thanks for sharing how to do this! That Grady guy also seems really cool.

Collapse
 
cassidoo profile image
Cassidy Williams

And sooooo smart

Collapse
 
boly38 profile image
Brice • Edited

There is also more commit msg keywords convention.. for example :

Closes: $BUGNUMBER
Reviewed-by:
Suggested-by:
Tested-by:
Thanks: msg
(...)
Enter fullscreen mode Exit fullscreen mode

Cf. Src old wiki, and git trailer SO post

Collapse
 
jake_nelson profile image
Jake Nelson

Can you do this trailer action on an amend? Is it just containing that other email in the required format within the commit body message that does this or is it some kind of annotation?

Collapse
 
valscion profile image
Vesa Laakso

Yeah I think you can do a

git commit --amend --trailer 'Co-authored-by: name <person@example.com>'
Enter fullscreen mode Exit fullscreen mode

to get what you want.

And couple that with --reuse-message=HEAD to only add the trailer to the most recent commit and not even trigger an editor to open up:

git commit --amend --trailer 'Co-authored-by: name <person@example.com>' --reuse-message=HEAD
Enter fullscreen mode Exit fullscreen mode