How to use multiple users with git
Did you ever switch from a project for work to a private project and in the rush of coding you committed and pushed your code with the wrong user? Keep reading how to avoid this situation by setting up different users for different repositories.
So we are not going to start in a cage but let’s pretend we are Tony Stark and we are working on a private project and on the side we do commit for a little iron suit we are building. Let’s create the following structure of folders and files in our root directory. The .gitconfig file in the root directory might already exist. If not create it as well.
├── Root/
│ ├── .gitconfig
│ ├── Private/
│ │ ├── .gitconfig
│ ├── IronMan/
│ │ ├── .gitconfig
Let’s start with the the config files in our subfolders.
In the Private folder ~/Private/.gitconfig, add the following:
[user]
name = Tony Stark
email = stark.tony@starkindustries.com
In the IronMan folder ~/IronMan/.gitconfig, add the following:
[user]
name = Iron Man
email = stark.tony@starkindustries.com
Finally, in the .gitconfig in the root folder we will put all the things together.
[user]
name = Default Name
email = default@mail.com[includeIf "gitdir:~/Private/"]
path = ~/Private/.gitconfig
[includeIf "gitdir:~/IronMan/"]
path = ~/IronMan/.gitconfig
The user here is your default user for all git configs. For all projects in IronMan the ~/IronMan/.gitconfig will be applied and the user will be overwritten.
That’s it. You do not have to worry about commiting with the wrong user anymore. If you have any other ideas that simplify working with git let us know and give us feedback if you like to.
Enjoy coding.