How to separate your work and personal commits in git (Fix and prevent)
Tags: git, github, beginners, programming
Published: 2022-04-21 17:35:24

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.

{% embed https://gist.github.com/Alissonsleal/c8ac6b233800adb374dcd473cdbdb501 %}

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:

```sh
# ~/work/gitconfig
[user]
  name = "Your Work Name"
  email = "your@work.email"
```

```sh
# ~/personal/gitconfig
[user]
  name = "Your Personal Name"
  email = "your@personal.email"
```

### 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:

```sh
[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"
```

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

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

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

### 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:

```sh
# ~/personal/gitconfig
[core]
	sshCommand = ssh -i ~/.ssh/personal
[user]
  name = "Your Personal Name"
  email = "your@personal.email"
```

```sh
# ~/work/gitconfig
[core]
	sshCommand = ssh -i ~/.ssh/work
[user]
  name = "Your Work Name"
  email = "your@work.email"
```

That's it!

Sources:

- `man git config` inside the `includes` part
  - https://www.git-scm.com/docs/git-config#_includes
- https://meleu.sh/git-multiconfig/ in pt-br
- https://www.git-tower.com/learn/git/faq/change-author-name-email/

If you wanna check out what I'm currently working on: [brapi.dev](https://brapi.dev)