Skip to content

Instantly share code, notes, and snippets.

@iandouglas
Last active March 21, 2024 12:44
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save iandouglas/6ff9428ca9e349118095ce7ed4a655bf to your computer and use it in GitHub Desktop.
Save iandouglas/6ff9428ca9e349118095ce7ed4a655bf to your computer and use it in GitHub Desktop.
Intro to Co-Authored Git Commits

Intro to Co-Authored Git Commits

Why

In student projects, I commonly look at contribution graphs to see how much code they worked on, to get an idea if one student is doing more work than others on the project. This can be used for coaching and performance grading.

How

Start with your ~/.gitconfig file in your home folder. Add these lines:

[commit]
  template = ~/.gitmessage

Next, create a file called ~/.gitmessage, also in your home folder, that looks like this, including two blank lines at the top:

(blank line)
(blank line)
Co-authored-by: Megan McMahon <32604200+memcmahon@users.noreply.github.com>

don't actually put (blank line) in the file, just make it a blank link! :)

You can add as many Co-Authors as you like, one per line, but the format must follow the above. When in a team project, please add ALL of your teammates. Do NOT include yourself, though.

GitHub has built-in co-authoring contributions, but it requires that you know the email address used by the co-author. Each co-author can find their "anonymous" GitHub email address by navigating to their email settings page under "Keep my email address private."

If you're trying to set this up yourself and can't reach your co-authors, you can build their anonymous email this way: their github user_id, a plus sign, their github username, and finally @users.noreply.github.com.

To find their github user_id, visit their profile, right-click on their profile photo and "inspect" the image or view the page source. The profile picture URL will look something like this: https://avatars2.githubusercontent.com/u/32604200 The number at the end is their user_id

Set up a better commit message editor

By default, git commit will use an editor called vim to edit commit messages. Let's use Atom instead.

In your ~/.gitconfig file, add the following lines:

[core]
  editor = atom --wait

Your .gitconfig file might look like the following now:

[user]
  email = ian.douglas@iandouglas.com
  name = ian douglas

[commit]
  template = ~/.gitmessage

[core]
  editor = atom --wait

How to use it

After you've staged files to commit (I recommend git add -p), simply type git commit and hit enter. Atom will start up, include everything from your .gitmessage file. Enter new content on line 1, keep a single blank line between your commit message and your co-authors, remove any co-authors that did NOT contribute, save and close the file, and you've just co-authored your work.

If you're used to using git commit -m on the command line, it will NOT use your .gitmessage file. This will allow you to make SOLO contributions to the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment