Colin Bacon, web developer.

The pitfalls of using a global author identity in Git

The pitfalls of using a global author identity in Git

Typically identity details (name and email) used when committing changes to a repository are set globally. Depending on the types of projects you are working across this can be more of a hindrance than a help.

Background

When you set up Git on your computer one of the first things you'll likely do is use git config to set your user name and email (author identity). Both are used to associate your commits with you.

Typing git log in the terminal will display your commit details.

A git commit message displayed in a terminal

Alternatively a git GUI client will display the same information in a slightly prettier format.

A git commit message displayed in Fork git client

Setting your identity globally

Git allows global settings. You can set your identity once and it will be used across all repositories on your local machine. This is very convenient, using just two commands.

git config --global user.name "Colin Bacon"
git config --global user.email "my-email@address.com"

Setting your identity for a single repository

To set the author identity for a single repository, in the directory for the repository, run the same commands but omit --global.

git config user.name "Colin Bacon"
git config user.email "my-email@address.com"

What's wrong with having a global identity?

Adding your author identity globally is much easier but it is something that has tripped me up enough times in the past to make me change. If you answer yes to one these statements below, maybe you should too.

  • You have a mixture of work projects and personal projects on the same machine
  • You have more than one development machine
  • You are a little OCD about your Git commits

Different projects

I get miss matches all the time. I have personal projects and work projects all on the same machine and I invariably get it wrong by committing with the wrong identity. This is especially important if you are committing to public repositories that are visible to all. Maybe your employer doesn't want their company name associated with public repos.

Multiple machines

You have a work laptop and a personal laptop, maybe you work on a project across both machines. Again you don't want to accidentally commit with two different identities.

Particular about commits

On personal (private) projects it does not matter as much but it does annoy me to see 3 different contributors when they are all actually me. Yes I have a project that looks like that.

What email to use?

I like to use the GitHub no-reply email. Even for personal projects that are not public (if I change my mind later it's OK). One email that can link back to me via GitHub rather than a personal email and be open to spam. Plus it doesn’t matter if your personal email changes.

What if you have not set up an identity

If you have neither set up a global identity or a repository specific one, when you commit you will get the following message.

Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate.

Git will perform the commit, setting the author to your machine username and hostname.

Changing the author identity for the last commit

If you have committed with the wrong author identity and want to change it then you can do this in two ways.

1.

git commit --amend --author"Colin Bacon <me@example.com>"

This will amend the author of the last commit.

2.

First set the author identity using git config commands. Then run

git commit --amend --reset-author

This will amend the author of the last commit with the identity you just set.

Both of these are fine to do if you have only committed locally. If you have already pushed your commit then I would advise against this as it could cause problems for your fellow collaborators.

Options

Now that we have begun to understand the common pitfalls of setting a global author identity, what are our options?

Per repository

As we have seen we can set the author identity for each repository. The downside of this is we need to do it each time we work on a new repository. If we forget, Git will auto create the identity and still commit. So you could still end up committing with the wrong author identity.

Multiple configs

It is possible to use multiple config files as described in this article. To summarise, create a folder for each different author identity you use. These folders will contain your repositories. For example, you could have a work and a personal folder.

Now create a gitconfig file for each folder and save it to the same location as the global .gitconfig file. Example naming for the files are .gitconfig-personal & .gitconfig-work. In these files add the author identity information.

The clever part is that these config files can be conditionally included in .gitconfig based on the repository directory (hence the separate folders). e.g.

// .gitconfig
[includeIf "gitdir:~/personal/"]
  path = ~/.gitconfig-personal
[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig-work

As long as your repository is located under the correct folder you will use the correct configuration. This is a nice solution to the problem, it gives you the ease of global configuration, but allows you to have different configurations depending on the project you are working on.

Summary

We have seen some of the issues that can arise from using a global author identity. Repositories can be configured individually but over a large number of repositories this could become a bit of a chore. However using multiple config files seems to be a good approach to solve this issue. Either way be mindful of the author identity you are committing with especially for new repos or when working from a different machine.