Multiple Git accounts on the same machine.
Introduction
When we talk about Git and code versioning in the world of software development, many details often go unnoticed. Recently, I found myself facing a problem: how to maintain correct versioning between the code I send to the company’s repository and my own study projects? Every time I made a commit, the company email went to my personal study repository, which not only was incorrect but also became bothersome over time. However, my case is still mild. Imagine you’re providing services for more than one company or working as a freelancer; you need to commit with the correct name and email for the final repository, as versioning code is also a passive document for auditing and control. After much research, I discovered this tip that I want to share with you.
Git Configuration Usually, when we create a commit, the log generated for it looks like the following code:
commit 82b23142b6df8149787760c82d2b728f8fb0ee35 (HEAD -> master, origin/master, origin/HEAD)
Author: Leonardo <mypersonalemail@gmail.com>
Date: Sun Apr 20 23:31:04 2024 -0300
Fix scrolling issue
Let’s suppose this is a commit for the work repository, not personal. Therefore, my name should be Leonardo Herdy Marinho, and the email something like leonardomarinho@company.com. If you notice, there’s a separate part specifically for the Author. If you don’t configure it, Git will take whatever information is available on the computer, and most of the time it won’t correctly put either the name or the email. To set up a single global account is super simple, just type:
git config user.name --global "Leonardo"
git config user.email --global "mypersonalemail@gmail.com"
But what if I need to change the company email for Project X? The non-automated way is to manually set it within the project:
git config user.name "Leonardo Herdy Marinho"
git config user.email "leonardomarinho@company.com"
But, here comes the fateful question…
"But what if there are multiple projects for the company, do I need to do this in each project folder?"
The answer before this post would be yes, you need to define manually. But as you know, programmers are a class of lazy people who will always automate the boring work to be done.
Configuring different emails
When Git is about to sign a commit, it first looks for the information inside the .git folder within the project. If nothing is defined locally, it then searches for the global configuration, which is stored in a file called .gitconfig in the user’s root folder. And this is where we’ll make a move for Git to read what we want it to read. I usually have a folder structure like this:
~/Sites
/work # work projects
/studies # studies projects
Since it’s a straightforward pattern, it’s easy for Git: if it’s in the work folder, use my professional email; if it’s in the studies folder, use the personal one. To achieve this, we need to create two files:
.gitconfig-work
.gitconfig-studies
With the following content:
# .gitconfig-work
[user]
email = leonardomarinho@company.com
# .gitconfig-studies
[user]
email = mypersonalemail@gmail.com
Remember that I’m only showing the email here, but you can also configure other settings such as specific aliases or different names separately in these files. But with just this, it still won’t work. We need to tell Git when to read one or the other, so we’ll edit the original .gitconfig (in your user’s root) to look like this:
[user]
name = Leonardo Herdy Marinho
[init]
defaultBranch = main
[includeIf "gitdir:~/Sites/work/"]
path = .gitconfig-work
[includeIf "gitdir:~/Sitest/studies/"]
path = .gitconfig-studies
Note that I kept my name and the init branch globally, and I used an includeIf to modify only what I needed.
Important: Sometimes relative paths don’t work (~/Sites/…), if that’s your case, try the absolute path, something like (/Users/my_user/Sites/work).
That’s all, folks!
I hope it’s as useful for you as it was for me. Now you can keep your work organized and document your commits correctly. If this post helped you in any way, don’t forget to like or comment :).