Home > Net >  How to switch git email based on host?
How to switch git email based on host?

Time:12-17

I have accounts in three different Git service providers - GitHub, GitLab and BitBucket. I want to ensure that my commits are signed via SSH in all of the mentioned providers, which is not possible since I have assigned a GitLab email at the moment, and all of my accounts use unique emails. This only verifies signed commits for GitLab successfully. How do I make it so that GitHub/BitBucket repositories will be assigned to their respective email?

Haven't found anything online that deals with this. There is a "gitdir" method, but I am not looking for this. I am expecting that Git understands what host I am using (if on GitHub, use [email protected], if on GitLab, use [email protected] and so on), and based on that, assign emails.

CodePudding user response:

Git allows you to set the default email address for new commits in a local repository clone via the following command:

$ git config --local user.email "[email protected]"

So as long as any given local repository of yours is only pushing to a given hosting service, then you should be able to assign the appropriate email to the commits.

Does that answer your question?

CodePudding user response:

Unfortunately, Git config does not support conditionals directly, but includes other config files. I was able to resolve my issue by using hasconfig:remote.*.url condition for includeIf. You may remove the email variable under [user] block - this is your global email. Create three new blocks:

.gitconfig

...
[includeIf  "hasconfig:remote.*.url:[email protected]:**/**"]
path = .github
[includeIf  "hasconfig:remote.*.url:[email protected]:**/**"]
path = .gitlab
[includeIf  "hasconfig:remote.*.url:[email protected]:**/**"]
path = .bitbucket
...

Now create three config files .github, .gitlab and .bitbucket:

github

[user]
  email = [email protected]

gitlab

[user]
  email = [email protected]

bitbucket

[user]
  email = [email protected]
  • Related