DEV Community

Cover image for How to separate your Git for work and Git for personal
Fadil Natakusumah
Fadil Natakusumah

Posted on • Updated on

How to separate your Git for work and Git for personal

Have you ever run into a problem where you want to push your code to a company repo, but you don't want to push using git with your personal email?

Here's how easy you can separate your Git for work and Git for personal

First, you must make new SSH Keys with your work email and add it to your work account in your company repo. Make sure you name your ssh key files to be something like id_ed25519_work to differentiate your SSH key for personal and work. Here's how to generate SSH keys.

If you open your git config in ~/.gitconfig, you'll probably have something like this.

[user]
    name = yourusername
    email = youremail@mail.com
Enter fullscreen mode Exit fullscreen mode

In Windows you can find your git config file in C:\Users\[YOURUSERNAME]\.gitconfig.

That is for your personal git account. For your working git account, you can make another git config file, for example .gitconfig-work, and you can fill your file with this

[user]
    name = yourusername
    email = yourworkemail@company.com
Enter fullscreen mode Exit fullscreen mode

In your original git config file, add this line

[user]
    name = yourusername
    email = youremail@mail.com

[includeIf "gitdir/i:[location to your company's project folder]/"]
    path = ~/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

for example, it would be like this.

[user]
    name = yourusername
    email = youremail@mail.com

[includeIf "gitdir/i:~/working/projectA/"]
    path = ~/.gitconfig-work
Enter fullscreen mode Exit fullscreen mode

and update your gitconfig-work to be like this

[user]
    name = yourusername
    email = yourworkemail@company.com

[core]
    sshCommand = "ssh -i ~/.ssh/id_ed25519_work"
Enter fullscreen mode Exit fullscreen mode

That's it! So, whenever you're in your company's project folder, your Git will automatically use your work email and username with your seperate ssh keys.

You can test your ssh file by typing this in your terminal.

ssh -i ~/.ssh/id_ed25519_work -T git@gitlab.com
Enter fullscreen mode Exit fullscreen mode

and you can test if it's work by typing this from your company's project folder and check if your work email comes up.

git config --show-origin --get user.email
Enter fullscreen mode Exit fullscreen mode

You can change that gitlab.com to github.com if you're using github.

You might want to close all the terminals, or try to logout/restart your computer if it's not working.

Hope this help and Thanks for reading!

Oldest comments (1)

Collapse
 
johnjacobkenny profile image
Kenny John Jacob

I was spending close to an hour on this looking this up in multiple places. This post saved me from more hours of frustration. Thank you so much!

I used to have this working properly in Linux and so on Windows it was not working correctly, and because of a missing trailing slash. Sad life.