How to use multiple users with git

Manuel Kunz
Feb 11 · 2 min read
Image for post
Image for post

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.

StepUp Development

Best practices and experiences around mobile development and more

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store