How to setup git author details for different business clients
On git, every commit has author info attached to it. It is usually a name, and email. If you are working with multiple clients, it can be a legal problem if you push a code with a different email. Been there...
But even if you don't work with multiple clients, you still can work on personal side projects and company projects. It's important to differentiate those. You might open source your project and commit history will be public to everyone. You don't want to have a company email attached to it.
It's super-easy to set this up with git config so that you never think of that again.
On the repository level, it’s easy to set it with --local
parameter in the config. However, this is repetitive and you’ll forget it at some point.
What we need is a more general, and once per client set, approach, without tackling each repository individually.
1. Organize repositories on disk
Whether you’re setting git configs or not, having a directory structure like this helps with other stuff as well:
repositories
- clientA
+ longterm_repo
+ new_proj
- clientB
+ boring_project
+ mainenance_repo
2. Define client-specific git config files
Locate the directory where gitconfig
is located. The location of this file depends on your git installation. On macOS, it’s in the /Users/youruser/.gitconfig
.
Create additional config file(s) for every client, as in the snippet below.
- users
- youruser
.gitconfig
.gitconfig-companyA
.gitcontig-companyB
The content of client git config files is minimal and is shown below.
.gitconfig-clientA
[user]
name = Dusko Bajic
email = dusko@clientA.net
.gitconfig-clientB
[user]
name = Dusko Bajic
email = dusko@clientB.org
3. Edit git global config
Open .gitconfig
file in edit mode and add the following blocks.
[user]
name = Dusko Bajic
email = dusko@mail.com
[includeIf "gitdir:~/repositories/clientA/"]
path = .gitconfig-clientA
[includeIf "gitdir:~/repositories/clientB/"]
path = .gitconfig-clientB
With name
and email
, we’ve defined the default author setup for all repositories.[includeIf]
blocks are matching the client repositories directory, with the client git config file.
And that's it. Each repository will have correct author info, per client. You can check it out by navigating to /repositories/clientA/longtermRepo
and executing git config user.email
. With the setup example from above, it will print dusko@clientA.net
in the console.