Tipically, one of the first tools that a developer learns when starting the "dev journey" is git. For those who still don't know much about this little guy, it was created by Linus Benedict Torvalds, a Finnish software engineer, naturalized American, known as the creator of linux kervel. Torvald's idea was to have a simple versioning system that met three requirements that he himself considered indispensable (and that other software couldn't guarantee):
- It needed to be distributed;
- The performance had to be good;
- It needed to ensure that what was put in, was exactly what was obtained afterwards;
According to him, these three parameters were sufficient to discard virtually all versioning systems until then, so he decided to build his own system.
Everyone who has ever set up a local git user knows that it have no secrets. You define the user name and email and... that's it.
git config --global user.name "Tom Benevides" git config --global user.email "firstname.lastname@example.org"
From now on, all the repositories you create will use these credentials.
mkdir new_repo && cd "$_" git init touch text.txt git add text.txt git commit -m "new file"
And if I take a look at the git log, this will be the result:
commit 036573401e5788917383a27fb6c2acf607f5e441 (HEAD -> master) Author: Tom Benevides <email@example.com> Date: Sun Feb 7 13:50:05 2021 -0400 new file
So, everything is fine with our configuration but... what if my project uses a different account?
We'll now create another repository. This is going to be the work_repo. It actually uses a different email account because it's a company project and not a personal project like new_repo.
cd ~ mkdir work_repo && cd "$_" git init touch text.txt
If we commit the changes now, my personal account (system default) will be registered, but I need to use the work email, firstname.lastname@example.org. So, we need to configure the new credentials in the repository.
git config user.email "email@example.com" git add text.txt git commit -m "new file"
Now, when committing, my registered email will be the work email.
commit f47e5c7140296c9fbe1f4fb001149b04b329b655 (HEAD -> master) Author: Tom Benevides <firstname.lastname@example.org> Date: Sun Feb 7 14:05:02 2021 -0400 new file
All right, happy ending? So-so. Now, imagine that you have several work repositories and several personal repositories. It will be a bit annoying that you have to configure the correct credential in each of the repositories. If your work account email changes or you have a new git account? You'll need to make the change in all repositories. Lot of job right? There're people who don't think so.
Personally, what I can do to make my life simpler, I’m doing and a nice idea would be to configure git so that every time I create a repository in a given folder, the credentials I want are automatically assigned to it. And look how cool; git does that!
The idea is pretty simple: we'll have two base folders (Work and Personal) where the repositories will be stored according to their respective origin. All repositories within each base folder will use the same credentials, different from the default and without configuration by repository. Structure proposal below.
Projects ├── Personal └── Work
Now, we need to tell git that the Personal folder repositories will use the email "email@example.com" and not the default email we set up earlier. To do this, in your home (
cd ~), create a file called
.gitconfig-personal and as content, simply recipient of the email.
[user] email = firstname.lastname@example.org
Once we set up the Personal folder credential, we will create a
.gitconfig-work file to set up work credential.
[user] email = email@example.com
With our config files created, we'll now edit the
.gitconfig file found in the user home (
cd ~) and let git know that whenever there is a repository inside the Personal folder, it must use the credential of the
.gitconfi-personal file and every time there is a repository in the Work folder, git must use the credential of the
[user] name = Tom Benevides email = firstname.lastname@example.org [includeIf "gitdir:Projects/Personal/**"] path = .gitconfig-personal [includeIf "gitdir:Projects/Work/**"] path = .gitconfig-work
... and voilà! Now, the work repositories only need to be inside the
~/Projects/Work directory and all commits will use the correct credentials, without any configurations by repository. The same goes to the Personal folder repositories. And if at any point, your work (or personal) git account email changes, just change the
.gitconfig-X file corresponding to the folder and all repositories inside will use the new credential.