How to setup 2 separate Git profiles on a single device?

sohammondal profile image Soham Mondal ใƒปUpdated on ใƒป2 min read

I work on a single device and use it for both my office work and side/pet/personal projects. Default git credentials for the device is obviously the one provided by my office. Now, this scenario has happened to me a lot of times -

  • My office work is done
  • I am super excited about a personal project idea
  • So I set up a remote git repository
  • Set up the project boilerplate locally
  • Fire git init, git remote add, git add, git commit & PUSH!!!


And it is then when I realize I have pushed code to my personal git with wrong (office) credentials. And I start all over again.

Do let me know if you have ever faced a similar situation like mine in comments!

So, now what is the solution? How do I have separate git config on a single machine? The answer is pretty simple. You need to have two separate Git config files. But, how ? Read on...

*** Before starting make sure you have git version 2.13 or higher installed in your system. ***

$ git --version
git version 2.14.2

Now with the prerequisites set, let's begin -

  • Your .gitconfig resides in your home directory. For Windows users, it will be in the path C:\Users\[YOUR_NAME]. Go to that directory.
  • Create two separate folders like My_Office_Workspace & My_Personal_Workspace.
  • Create two separate .gitconfig files, one for office and another for personal usage in the path C:\Users\[YOUR_NAME]. Let's name them .gitconfig-office & .gitconfig-personal.

The contents of .gitconfig-office -

# This is Git's per-user configuration file.
email = your.name@company.com

The contents of .gitconfig-personal -

# This is Git's per-user configuration file.
email = your.name@gmail.com
  • After that's done, open up the .gitconfig file, and make the necessary changes. It should look like -
[includeIf "gitdir:~/My_Office_Workspace/"]
  path = .gitconfig-office
[includeIf "gitdir:~/My_Personal_Workspace/"]
  path = .gitconfig-personal

Configuration done! Now let's verify.

  • Goto My_Office_Workspace and clone a project from your office GitHub, BitBucket or GitLab. Let's call the project My_Office_Project and do the following -
$ cd My_Office_Workspace/
$ git clone https://github.my_office.com/VeryImportant/My_Office_Project.git
$ cd My_Office_Project/
$ git config user.name
  • You can also do the same for your personal projects under My_Perosonal_Workspace, but you will see you the output for the last command git config user.name is YOUR_PERSONAL_GIT_USERNAME.


Woohoo, it's done! No more messing up. ๐Ÿ˜ƒ


If your projects are in a different drive on Windows, for eg. D or F, then you can configure the .gitconfig like this

[includeIf "gitdir:D:/My_Office_Workspace/"]
  path = .gitconfig-office
[includeIf "gitdir:F:/My_Personal_Workspace/"]
  path = .gitconfig-personal

Liked this post? Share it โ†ฉ๏ธ or maybe give a heart ๐Ÿงก.

I am Soham Mondal, a developer from India. You can follow me on LinkedIn, Instagram, or GitHub. You can learn more about me on my website. I am currently open to new projects & job offers.


Editor guide
yutaoyan profile image

This is perfect!
Just incase anyone who want to git clone a private repo on your secondary account and it shows fatal: repo not found
Clone with the following:
git clone username@github.com/repoName
Now it will prompt you for password

sohammondal profile image
Soham Mondal Author

Thank you for pointing that out! :)

techdevasif profile image
Asif Kamran Malick

Very helpful. Thanks for it.

sohammondal profile image
Soham Mondal Author

Thanks Asif!

siwalikm profile image
Siwalik Mukherjee

This is such a relatable problem Soham. Nice article and congrats on your first post.

sohammondal profile image
Soham Mondal Author

Thank you! :)

abhayaagrawal1 profile image
Abhaya Agrawal

I tried this it's not working for me it's always showing one user details only.
can you help me?

sohammondal profile image
Soham Mondal Author

Hey, sorry for the delayed response. I will try to help you.

What OS are you on and also please share your git version and .gitconfig file contents.