How to setup 2 separate Git profiles on a single device?
Tags: git, github, bash, hack
Published: 2020-04-09 16:27:26


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!!!

![facepalm](https://media1.tenor.com/images/06655070b3cc8faaff4824eee848adc0/tenor.gif?itemid=5436796)

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...

*** <b>Before starting make sure you have git version 2.13 or higher installed in your system.</b> ***
```bash
$ 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` -
```bash
# This is Git's per-user configuration file.
[user]
name = YOUR_OFFICE_GIT_USERNAME
email = your.name@company.com
```
The contents of `.gitconfig-personal` -
```bash
# This is Git's per-user configuration file.
[user]
name = YOUR_PERSONAL_GIT_USERNAME
email = your.name@gmail.com
```

- After that's done, open up the `.gitconfig` file, and make the necessary changes. It should look like -

```bash
[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 -
```zsh
$ cd My_Office_Workspace/
$ git clone https://github.my_office.com/VeryImportant/My_Office_Project.git
$ cd My_Office_Project/
$ git config user.name
YOUR_OFFICE_GIT_USERNAME
```
- 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.

![success](https://media1.tenor.com/images/bff89595f10f1b07608dbe71ca67e75e/tenor.gif?itemid=4185058)

Woohoo, it's done! No more messing up. 😃

### Update

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

```bash
[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](https://sohammondal.com), a developer from India. You can follow me on [LinkedIn](https://www.linkedin.com/in/soham-sm-mondal/), [Instagram](https://twitter.com/sohammondal_), or [GitHub](https://github.com/sohammondal/). You can learn more about me on my [website](https://sohammondal.com/about). I am currently open to new projects & job offers.