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 :
1/home
2├── /user
3│ ├── /mycompany
4│ │ ├── /repo1
5│ │ └── /repo2
6│ ├── /othercompany
7│ │ ├── /repo3
8│ │ └── /repo4
9│ └── /repo5
What we want is :
repo1andrepo2to usejohn.doe@mycompany.comrepo3andrepo4to usejohn.doe@othercompany.comrepo5to usejohn.doe@perso.com
Your main .gitconfig file located at /home/user/.gitconfig should look like this :
1[user]
2 name = John Doe
3 email = john.doe@perso.com
4
5[includeIf "gitdir:/home/user/mycompany/"]
6 path = /home/user/mycompany/.gitconfig
7
8[includeIf "gitdir:/home/user/othercompany/"]
9 path = /home/user/othercompany/.gitconfig
⚠️ For Windows users, replace backslashes with slashes in the paths :
1[includeIf "gitdir:C:/Users/myuser/mycompany/"] 2 path = .mycompany/.gitconfig
Now, create a .gitconfig file in each company’s directory with the specific email configuration :
/home/user/mycompany/.gitconfig:
1[user]
2 email = john.doe@mycompany.com
/home/user/othercompany/.gitconfig:
1[user]
2 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 :
1/home
2├── /user
3│ ├── .gitconfig (Main configuration)
4│ ├── /mycompany
5│ │ ├── .gitconfig (Company-specific configuration)
6│ │ ├── /repo1
7│ │ └── /repo2
8│ ├── /othercompany
9│ │ ├── .gitconfig (Company-specific configuration)
10│ │ ├── /repo3
11│ │ └── /repo4
12│ └── /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.