Setting Up Multiple Git Users: A Practical Guide for Developers

Manually switching users in each repository can become a tedious and error-prone process. Today, we want to share this article with you that will guide you on how to efficiently set up multiple Git users. With these tips, you'll optimize your workflow and ensure that your commits are made under the correct account, depending on the host and the project. Keep reading to discover how to simplify this task and enhance your productivity!
Let’s get started step by step:
Configure the File
First, you need to associate specific SSH keys with each host. Edit or create the file with:
nano ~/.ssh/config
Add the following configurations for different platforms and accounts:
# Personal GitHub
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
# GitHub for the "Apple" project
Host github-project-apple
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_apple
# Company GitLab "Tomato"
Host gitlab-company-tomato
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_tomato
Explanation:
- Host github-personal and similar entries: Custom aliases for each account, typically linked to the project or company (e.g., gitlab.companytomato.com, github.projectapple). Otherwise, it defaults to the platform (github.com, gitlab.com).
- HostName: The actual domain of the Git service.
- IdentityFile: The SSH key corresponding to that account.
If you don’t have SSH keys created and added in GitHub or GitLab, here’s a brief explanation of how to do it:
Use the following command, specifying the identifier you want your key to have with the -C
flag and the name and location of the file with the -f
flag. The keys should be in the .ssh
folder.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_personal
Make sure to replace "your_email@example.com"
with your actual email and adjust the file name accordingly for different accounts.
ssh-keygen -t ed25519 -C "[your_email@example.com](mailto:your_email@example.com)" -f ~/.ssh
/my_personal_key
This will generate two files: one containing the private key and another with the public key (ending in .pub). You will need to add this public key to the platform you are going to use (GitHub, GitLab).
Next, make sure to protect the permissions of the private key:
chmod 600 ~/.ssh/my_personal_key
Add the SSH Key to the Agent
First, check if the SSH agent is running:
eval "$(ssh-agent -s)"
Then, add the private key to the agent:
ssh-add ~/.ssh/my_personal_key
If prompted, enter the passphrase for the key (if you set one).
Verify that the Key is Loaded
To check that the key was added correctly, use:
ssh-add -l
This command will list the identities added to the SSH agent, confirming that your key is loaded and ready to use.
Setting Up Multiple Users in Git
To use a different Git user for each project, I recommend creating a folder structure where the Git configuration will be based. Then, associate the contents of each of these folders with the respective Git user in the .gitconfig file. This user will be used for commits, while a general user will remain for any other repositories outside this folder structure.
📂 home
├── 📂 personal_projects
│ ├── 📂 repository-1
│ ├── 📂 repository-2
│ ├── ...
│
│
├── 📂 Apple-projects
│ ├── 📂 repository-1
│ ├── 📂 repository-2
│ ├── ...
│
│
├── 📂 Tomato_projects
│ ├── 📂 repository-1
│ ├── 📂 repository-2
│ ├── ...
│
│
├──📄 .gitconfig-personal
├── 📄 .gitconfig-tomato
├── 📄 .gitconfig-apple
Now, let's edit your global Git configuration
git config --global --edit
Add the following rules that define specific users based on the location of the projects:
[user]
name = General User email = user@general.com
[includeIf "gitdir:~/personal_projects/"] path = ~/.gitconfig-personal
[includeIf "gitdir:~/personal_projects/"] path = ~/.gitconfig-apple
Create Specific Configuration Files
Now, create the configuration files for each user:
For Personal Projects ~/.gitconfig-personal
[user]
name = User Personal
email = user.personal@email.com
Para el proyecto "Manzana" ( ~/.gitconfig-apple ):
[user]
name = User Apple
email = user.apple@email.com
For the Company "Tomato" ( ~/.gitconfig-tomato ):
[user]
name = User Tomato
email = user.tomato@email.com
Cloning and Working with Repositories
When cloning a repository using SSH, make sure to use the appropriate user and keys based on the folder where the repository will be downloaded.
Verify the Configuration
To ensure you are using the correct configuration in a project, navigate to the project's folder and run:
git config user.name
git config user.email
To check which SSH key is being used, run:
ssh -T git@github.com
ssh -T git@gitlab.com
With this configuration, you can manage multiple Git accounts without the hassle of manually switching users or SSH keys for each repository. Simply define the necessary aliases in your configuration and set up the specific configurations for each project. This way, you can focus on what truly matters: developing and delivering quality code.
Happy coding!
If you have any questions or need further assistance, feel free to contact us at Digitalprojex.
We are here to help!