# Git For 2

## Introduction

When working with Git, you usually configure your user details (like name and email) globally. This means the same configuration applies to all repositories on your machine. However, there are situations where you might need different configurations for different directories. I didn’t have to worry about this because I used to dual-boot Windows and Ubuntu, so work and play were completely separate. However, due to some circumstances involving a failed M2 to SATA adapter, I now have to do everything from one setup.

![a cartoon character with the words what a drag written on the bottom](https://media1.tenor.com/m/BiiTsQ_0j7AAAAAC/mendokusai-what-a-drag.gif align="left")

As a result, I may or may not have committed code to a work repo with my personal email. I’m going to go over how **not** to do this.

## What Does Separating Git Configurations Mean?

In Git, configurations can be applied at three levels:

1. **System**: Configuration applies to all users and repositories on the system.
    
2. **Global**: Configuration applies to all repositories for the current user.
    
3. **Local**: Configuration applies only to a specific repository.
    

Separating Git configurations means setting different Git settings (such as username, email, signing key, etc.) for specific repositories or directories rather than using a single global configuration. This allows you to use different identities or settings depending on the project or the directory you work in.

## Why Separate Git Configurations?

There are various reasons why you might want to have different Git configurations for different directories:

1. **Working with Multiple Accounts**: If you are working on both personal and work-related projects, you may want to use separate Git configurations for each. For example, you may want to commit to your personal projects using your personal email, but use your work email for company-related repositories.
    
2. **Different Signing Keys**: Some projects might require you to sign your commits using a specific GPG key. This is common in open-source projects where verifiable commits are important.
    
3. **Custom Behaviors for Specific Projects**: You may want to enforce specific Git behaviors (e.g., using a specific diff tool or merge strategy) only for certain directories, such as for large repositories with unique requirements.
    

## Advantages of Separating Git Configurations

* **Identity Control**: You can use different usernames and emails for work and personal projects, ensuring that your commits are always attributed to the correct account.
    
* **Project-Specific Settings**: You can tailor Git's behavior for each repository, setting up preferences like commit signing, diff tools, or line-ending rules.
    
* **Security**: For sensitive projects, you may want to apply stricter rules like requiring GPG signing for each commit.
    
* **Efficiency**: Automating configuration means you won't need to remember to manually switch identities or settings before making a commit.
    

## Step-by-Step Guide to Separating Git Configurations

### 1\. **Check Your Current Global Configuration**

Before making any changes, it's a good idea to see what configurations you already have globally set up. Use the following command to view your global configuration:

```bash
git config --global --list
```

This will show you the current user name, email, and other settings applied to all repositories.

### 2\. **Set Up Local Configuration for a Specific Repository**

To set a configuration that only applies to a single repository, navigate to that repository and use the `git config` command with the `--local` flag. For example, to set a custom username and email:

```bash
cd /path/to/repository
git config user.name "Your Work Name"
git config user.email "your.work.email@example.com"
```

This will save the user name and email for that specific repository only, without affecting any other repositories.

### 3\. **Set Up Directory-Specific Configurations with Conditional Includes**

If you want to apply a Git configuration to all repositories within a certain directory, you can use conditional includes. This feature allows you to create configuration rules based on the directory structure.

Start by editing your global `.gitconfig` file:

```bash
nano ~/.gitconfig
```

Now, add the following to conditionally include configurations based on the directory:

```ini
[includeIf "gitdir:~/path/to/work/"]
    path = ~/path/to/.gitconfig-work

[includeIf "gitdir:~/path/to/personal/"]
    path = ~/path/to/.gitconfig-personal
```

This example ensures that:

* Repositories under `~/path/to/work/` will use a separate configuration from `~/path/to/.gitconfig-work`.
    
* Repositories under `~/path/to/personal/` will use the configuration from `~/path/to/.gitconfig-personal`.
    

Next, create the respective `.gitconfig-work` and `.gitconfig-personal` files with your specific settings:

For `~/.gitconfig-work`:

```ini
[user]
    name = "Your Work Name"
    email = "your.work.email@example.com"
```

For `~/.gitconfig-personal`:

```ini
[user]
    name = "Your Personal Name"
    email = "your.personal.email@example.com"
```

Now, any repository within the defined directories will automatically use the respective configurations.

### 4\. **Use SSH Keys for Authentication**

When working with different Git configurations, it’s often helpful to also use different SSH keys for authentication, especially if you’re using different Git services (e.g., GitHub for personal and GitLab for work).

* Generate a new SSH key for each account:
    
    ```bash
    ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
    ```
    
* Add the new SSH key to your SSH agent:
    
    ```bash
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa_work
    ```
    
* Update your SSH config to use the appropriate key for each service. Open the `~/.ssh/config` file and add entries like the following:
    
    ```bash
    # Personal GitHub account
    Host github.com-personal
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_personal
    
    # Work GitHub account
    Host github.com-work
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_work
    ```
    

When cloning repositories, you would use the appropriate host (e.g., [`github.com`](http://github.com)`-personal` or [`github.com`](http://github.com)`-work`), and Git will use the correct key for authentication.

### 5\. **Test Your Configuration**

Finally, make a test commit to ensure that the correct configuration is being used in each directory:

```bash
git commit --allow-empty -m "Test commit"
git log -1
```

Check the output to see if the correct user name and email were applied.

## Conclusion

Separating Git configurations for different directories is a useful practice, especially for developers who juggle multiple identities, projects, or repositories. It provides more flexibility and ensures that your commits are always associated with the correct identity and settings. By using local configurations and conditional includes, you can easily manage multiple accounts and project-specific Git preferences.

With the steps outlined above, you should be able to configure Git to better suit your development workflow. Happy coding!
