HomePostsGit configurations for multiple work environments

Git configurations for multiple work environments

Managing multiple git configurations can be streamlined by using conditional includes in your .gitconfig file. This allows you to specify different email addresses for commits in repositories belonging to different companies. Here’s how you can set it up :

Your directory structure is as follows :

/home
├── /user
│   ├── /mycompany
│   │   ├── /repo1
│   │   └── /repo2
│   ├── /othercompany
│   │   ├── /repo3
│   │   └── /repo4
│   └── /repo5

What we want is :

Your main .gitconfig file located at /home/user/.gitconfig should look like this :

[user]
  name = John Doe
  email = john.doe@perso.com

[includeIf "gitdir:/home/user/mycompany/"]
  path = /home/user/mycompany/.gitconfig

[includeIf "gitdir:/home/user/othercompany/"]
  path = /home/user/othercompany/.gitconfig

⚠️ For Windows users, replace backslashes with slashes in the paths :

[includeIf "gitdir:C:/Users/myuser/mycompany/"]
  path = .mycompany/.gitconfig

Now, create a .gitconfig file in each company’s directory with the specific email configuration :

/home/user/mycompany/.gitconfig:

[user]
  email = john.doe@mycompany.com

/home/user/othercompany/.gitconfig:

[user]
  email = john.doe@othercompany.com

With this setup, commits made in repo1 and repo2 will automatically use the mycompany email, and those in repo3 and repo4 will use the othercompany email. Commits in any other repositories will default to the personal email specified in the main .gitconfig.

The final directory structure with the configuration files will be :

/home
├── /user
│   ├── .gitconfig (Main configuration)
│   ├── /mycompany
│   │   ├── .gitconfig (Company-specific configuration)
│   │   ├── /repo1
│   │   └── /repo2
│   ├── /othercompany
│   │   ├── .gitconfig (Company-specific configuration)
│   │   ├── /repo3
│   │   └── /repo4
│   └── /repo5

This approach ensures that your commits are correctly attributed to you across different work contexts, keeping your contributions organized and properly associated with the respective companies.