How to use multiple accounts on the same computer to collaborate on GIT.
To help people who are using two or more different accounts on GIT.
In many cases, especially for those working from home office due to the circumstances of the pandemic today, there is a need for commits to be made at GIT, however for each project, using a repository and a different user.
In Project A, I’m contributing to GIT for a company where I’m part time freelance.
In Project B, I am contributing in GIT to another company in which I am consulting and defining architecture.
Finally, in Project C, I am using new knowledge that I study and testing new tools and frameworks and contributing to my personal GIT on Github.
Scenario you would like:
I cloned Project A and made changes. When committing, it should be done for "Repository A" and with "User A".
Similarly, the same should happen for work on Projects B and Project C.
The proposed way is for you to configure two or more different SSH keys. That way you can use them on a single user on the computer.
To do this, you must create the second key by executing the following command in a terminal:
ssh-keygen -t rsa -C "email@projetoB.com"
Running this command will display some messages and will be asked to select the directory that will generate your ssh key: Enter file in which to save the key (/Users/YourUser/.ssh/id_rsa).
At that point you must specify a different name for the file to each project (otherwise will be replaced the existing one), you can specify something: /Users/YourUser/.ssh/id_rsa_projectB.
Then repeat the process for linking the public key of this SSH key to the second user, and so on.
Thus now you have two or more SSH keys but need to set when to use each. To do this, create a file named config in the directory ~/.ssh with content like this:
#First ProjectA Account Host projectA.com HostName projectA.com User git IdentityFile ~/.ssh/id_rsa #Second ProjectB Acount Host projectB.com HostName projectB.com User git IdentityFile ~/.ssh/id_rsa_projectB #Third ProjectC Github Acount Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_github
You make the
clone in different directories and when you pushin each directory you will be using a different account.
Now imagine that we have the following repository to clone git@projectA.com:YourUser/test.git.
You can use one of the two hosts in the ~/.ssh/config.
When using git clone
git clone git@projectA.com:YourUser/test.git you’ll be using your default account.
To use the second account, just change the repository host: git clone git@projectB.com:YourUser/test.git.
In the directory of other users might need to configure and user.name and user.email. This can be done as follows:
git config --add user.name "ProjectB Acount"
git config --add user.email email@projectB.com `
I hope helps you. See you!