DEV Community

Cover image for How to separate your work and personal commits in git (Fix and prevent)
Alisson Leal
Alisson Leal

Posted on • Updated on

How to separate your work and personal commits in git (Fix and prevent)

You're a developer and you want to separate your work from your personal commits. You want to make sure that you don't accidentally commit to work using your personal email, or even worse, commit to an open source project using your work email.

Surely you know you can use git config user.name "Your Name" and git config user.email "your@email.com" to set your name and email for each project, but that gets tedious and error-prone if you work on a lot of repositories, I'm looking at your microservices each on their own repository and not on a mono-repo.

To fix this is actually very simple.

Fixing wrong email/name in commits

Just run this command in your terminal inside the repository folder, changing the variable WRONG_EMAIL to your wrong email, NEW_NAME to your new name and NEW_EMAIL to your new email.

This only fixes past problems, below this is a more general solution to prevent future problems.

Preventing wrong email/name in commits

Create a work and a personal folder

You must separate your work and personal projects in different folders, I for example have my work folder ~/work and my personal folder ~/personal. All the repositories that I work on are in the ~/work folder. All the repositories that are personal to me are in the ~/personal folder.

Create a gitconfig for each folder

Create a gitconfig file in both folders. The file must contain the following lines:

# ~/work/gitconfig
[user]
  name = "Your Work Name"
  email = "your@work.email"
Enter fullscreen mode Exit fullscreen mode
# ~/personal/gitconfig
[user]
  name = "Your Personal Name"
  email = "your@personal.email"
Enter fullscreen mode Exit fullscreen mode

Setup your global .gitconfig

You probably already have a .gitconfig file in your home folder, but if you don't, just create one in the root of your home folder.
It probably contains the following lines:

[user]
  name = "Your Name"
  # add your email if you will clone a repository in other folders other than your work or personal folder
  email = "your@default.email"
Enter fullscreen mode Exit fullscreen mode

To finish the setup, just add the following lines to your .gitconfig file:

# for your work repositories
[includeIf "gitdir:~/work/**"]
  path = ~/work/gitconfig

# for your personal repositories
[includeIf "gitdir:~/personal/**"]
  path = ~/personal/gitconfig
Enter fullscreen mode Exit fullscreen mode

How it works

When you clone a repository, the .gitconfig file in the root of the repository is read. If the repository is in your work folder, the gitconfig file in the work folder is read. If the repository is in your personal folder, the gitconfig file in the personal folder is read. The best part is if you have different ssh keys for your work and personal git, you can use setup them in each gitconfig file, like this:

# ~/personal/gitconfig
[core]
    sshCommand = ssh -i ~/.ssh/personal
[user]
  name = "Your Personal Name"
  email = "your@personal.email"
Enter fullscreen mode Exit fullscreen mode
# ~/work/gitconfig
[core]
    sshCommand = ssh -i ~/.ssh/work
[user]
  name = "Your Work Name"
  email = "your@work.email"
Enter fullscreen mode Exit fullscreen mode

That's it!

Sources:

If you wanna check out what I'm currently working on: brapi.dev

Top comments (0)