Little Git Tricks: Use .mailmap to Merge Different Authors

With every commit you do, Git not only registers the change in your code, but marks you as the author of this change as well. It uses the settings user.name and user.email to create an entry like this one in the log:

As long as you use Git only to manage your source code, this behaviour does not require any attention. You write your code, commit and push as you like, and everything works. However, if you intend to do any form of data mining on your Git repository, you need to look deeper on how authors of Git commits are tracked.

 

Is there a problem?

The settings for your name and email are just strings. You can set whatever you want and for Git those values are case-sensitive. A user.name of John and john are therefore two different users and Git will show them as two different authors. It can get even worse, then the email address is case-sensitive too:

The problem starts when you need to know how many commits are made by John. He may use different computers and have different settings on each of them. And even when he changes them all to an identical value, the commits already done will still show the old values.
Therefore, with every command you do to find out who has changed a file you will need to manually count the different authors. That is a lot of work, luckily you do not need to do that if you use a .mailmap file.

 

Use .mailmap to merge authors

Git will apply the settings in the .mailmap file before it shows you the output of any command you run. The content of .mailmap follows a simple template:

Name you want to keep <email> Name you no longer want <email>

For every user in your project that has multiple author identities, you can create an entry in the .mailmap file. You first write the author information you want to keep, then a space and then the author info you no longer want. In my example the .mailmap file looks like this:

If I now run the shortlog command again, I only get one entry per “real” author:

With this little trick you do not need to count or guess, Git just shows you what you need to know.

 

Conclusion

Merging authors using .mailmap is especially helpful when your team members use different machines or different Git clients. All you need to do is to update the file whenever a new combination turns up and everyone can focus on their work and not try to catch every Git misconfiguration. Try it when you need to use Git for more than just source control.

1 thought on “Little Git Tricks: Use .mailmap to Merge Different Authors”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.