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 kernel. 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.
A single git account on Linux: kid's stuff
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 "tombenevides@mail.com"
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 <tombenevides@mail.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?
A git account per repository: still piece of cake, but it can be a little tricky
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, tombenevides@work.com. So, we need to configure the new credentials in the repository.
git config user.email "tombenevides@work.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 <tombenevides@work.com> 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!
A git account per folder: the light at the end of the tunnel
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 "tombenevides@newmail.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 = tombenevides@newmail.com
Once we set up the Personal folder credential, we will create a .gitconfig-work
file to set up work credential.
[user] email = tombenevides@work.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 .gitconfig-work
file.
[user] name = Tom Benevides email = tombenevides@mail.com [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.
So... that's it! Try it and then tell me your experience in the comments, via Twitter or Instagram!
Top comments (8)
nice trick. I use this approach to distinguish personal and pro folders. It seems there are some caveats in specific cases but it does the job great.
Note that if the pattern ends with
/,
**
, are automatically added:Thanks for that tip =). I don't know why I used the "/ **" syntax, but I think I had some problems when I wrote without it, it didn't work so well. Reading your comment maybe I forgot the "/" and ended up just passing "... /Personal", but I can't remember now.
That's awesome, nice tip.
Welcome @aegiraexx hope you find all sorts of interesting things here.
Can you make one video on this , it will be easier to understand
yeah ... I'm not a "video guy" but if you tell me what confuses you, I can clarify. Which part did you have a hard time understanding?
When I trying to use 2 git account ,getting not access 403 error after all
Doing research and your blog it helps to me to manage two account . Thanks ☺️
Happy to help ( :