DEV Community

Cover image for How to use multiple users with Git
Camilo Martinez
Camilo Martinez

Posted on • Edited on

How to use multiple users with Git

Language: [πŸ‡ͺπŸ‡Έ] EspaΓ±ol - [πŸ‡ΊπŸ‡Έ] English


If you are working with multiple git users, for example, one for personal projects and another for a company.

So it's not a good idea to set a global user because all projects are going to use the same. And It will be easy to make a commit with the wrong user.

Amend

If it's your case, don't worry. It's not really a problem, because you can amend it with this command.



git commit --author="First Last <first.last@company.org>" --amend --no-edit 


Enter fullscreen mode Exit fullscreen mode

But seriously, how want to do that frequently? No one, for sure. It's a thing to use only in emergency cases.


Git Config

The first thing that I like to do is change the default core editor from vim to VSCode. It's an optional step.



git config --global core.editor 'code --wait'


Enter fullscreen mode Exit fullscreen mode

And then create two alias to easy access the configurations



alias gcg="git config --edit --global"
alias gcl="git config --edit --local"


Enter fullscreen mode Exit fullscreen mode

The difference between them is that global applies to all the git projects on your machine and local only applies to the current path.


Now we are going to learn how to use a different user for each project.

Remove Global πŸ‘Œ

This steep is recommended, but also optional. I will prefer not to have a default user for all projects, but it is up to you.

Now we are going to open the global config using the gcg alias or with the git config --edit --global command.

Remove all [credential] and [user] configurations.

The Hard way πŸ‘Ž

One option is after creating or cloning a repository, set manually the configuration with those commands.



git config user.name "<user>"
git config user.email "<user@mail.com>"
git config credential.username "<user>"


Enter fullscreen mode Exit fullscreen mode

It will be good if you don't have a specific path to which to create or clone the projects, but... it takes too much time and is easy to forget, believe me, moreover you are going to need to repeat the same with each repository that you clone.

The easy way πŸ‘

We are going to define a path where our projects are going to live and create a .gitconfig file for each user profile, as many as we need.



~
β”œβ”€β”€ .gitconfig <-- global
└── Developer/
   β”œβ”€β”€ personal/
   β”‚   β”œβ”€β”€ project_1/
   β”‚   β”œβ”€β”€ project_2/
   β”‚   β”œβ”€β”€ project_#/
   β”‚   └── .gitconfig <-- personal
   └── company/
       β”œβ”€β”€ project_1/
       β”œβ”€β”€ project_2/
       β”œβ”€β”€ project_#/
       └── .gitconfig <-- company


Enter fullscreen mode Exit fullscreen mode

Personal



# ~/Developer/personal/.gitconfig

[credential]
    username = <github-user>
[user]
    name = <github-user>
    email = <github-user>@users.noreply.github.com


Enter fullscreen mode Exit fullscreen mode

Business



# ~/Developer/company/.gitconfig

[credential]
    username = <user>
[user]
    name = <First Name and Last Name>
    email = <user>@company.org


Enter fullscreen mode Exit fullscreen mode

Global

Now we are going to open the global config using the gcg alias or with the git config --edit --global command.



# ~/.gitconfig

[includeIf "gitdir/i:~/Developer/personal/"]
    path = ~/Developer/personal/.gitconfig

[includeIf "gitdir/i:~/Developer/company/"]
    path = ~/Developer/company/.gitconfig


Enter fullscreen mode Exit fullscreen mode

So, it will take the user configuration profile per path, and you can create or clone projects inside each profile path without dealing with manual configurations and avoid using the amend command to fix mistakes.


Special thanks to feregri_no and ChrisFt25 from Twitter to show me these tricks.

That’s All Folks!
Happy Coding πŸ––

beer

Top comments (4)

Collapse
 
joancomasfdz profile image
Joan Comas Fernandez

@equiman thanks!

I ended up creating 2 different Dev Drives (learn.microsoft.com/en-us/windows/... 1 for personal and 1 for work. Then configuring a .gitconfig file on each drive's root.

Collapse
 
equiman profile image
Camilo Martinez • Edited

For Windows users, the global configurations is slightly different on paths.

[includeIf "gitdir/i:D:/Developer/personal/"]
    path = D:/Developer/personal/.gitconfig

[includeIf "gitdir/i:D:/Developer/company/"]
    path = D:/Developer/company/.gitconfig
Enter fullscreen mode Exit fullscreen mode
Collapse
 
syed_mohammadhassan_3c58 profile image
Syed Mohammad Hassan

Yeah, figured that out.

Collapse
 
syed_mohammadhassan_3c58 profile image
Syed Mohammad Hassan

@equiman , thanks a lot. this made life easy for multiple git users