Post

How to use two separate git profiles at once

How to use two separate git profiles at once

Two separate git profiles simultaneously (e.g. Work and Home)

Summary

How use two separate git profiles:

  • without risk of forgetting to perform a switch
  • without even needing to make switches
  • having company profile automatically being used in company repositories
  • having private profile being used in any other scenarios
  • configure it once, forget it forever

Solution

This solution assumes company has its own domain.

It binds work .gitconfig with company’s git remote URIs, and uses private.gitconfig elsewhere.

1
2
3
[user]
	name = Name Surname
	email = private@mail.com
1
2
3
[user]
	name = Name Surname
	email = work@company.com
1
2
3
4
5
6
7
## Default: Private
[include]
    path = .gitconfig-private

## Work: Company
[includeIf "hasconfig:remote.*.url:git@company.com*/**"]
    path = .gitconfig-company

And that’s all.

No more private commits in company repo :)

Credits

Solution is based on https://stackoverflow.com/a/77728201/10699128.

Resources

  • https://markentier.tech/posts/2021/02/github-with-multiple-profiles-gpg-ssh-keys/
    • Some more extended tutorial with different SSH keys

Other considered solution

Probably worse than this one above, but still may be helpful in some scenarios.

1
2
3
4
5
6
7
8
9
10
11
12
13
# Company
# D:/
[includeIf "gitdir/i:d:/"]
     path = .gitconfig-company

# Private
# E:/
[includeIf "gitdir/i:e:/"]
     path = .gitconfig-private

# github
[includeIf "hasconfig:remote.*.url:git@github.com*/**"]
     path = .gitconfig-private