How to switch git email based on host?
Asked Answered
K

2

7

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.

Koon answered 16/12, 2022 at 19:53 Comment(0)
R
3

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?

Rabid answered 17/12, 2022 at 5:42 Comment(0)
K
7

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 four new blocks for the same (two for each remote - one for HTTPS one for SSH). Here's an example of how my .gitconfig file looks like:

.gitconfig

[user]
  name = <Your name>

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

[includeIf "hasconfig:remote.*.url:https://github.com/**/**"]
    path = github

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

[includeIf "hasconfig:remote.*.url:https://gitlab.com/**/**"]
    path = gitlab

[init]
  defaultBranch = main

[core]
  editor = vim 

[color]
  ui = auto

[gpg]
  format = ssh 

[commit]
  gpgsign = true

Now create config files for the respective remotes. I've created .github, and .gitlab in the same directory as .gitconfig:

.github

[user]
  email = [email protected]
  signingkey = ~/.ssh/github-key.pub

.gitlab

[user]
  email = [email protected]
  signingkey = ~/.ssh/gitlab-key.pub

Edit 1: Initially, there were three different configs, one for GitHub, GitLab and Bitbucket, I've removed Bitbucket for simplicity.

Koon answered 17/12, 2022 at 7:52 Comment(5)
where you put User-Configuration email and password After Include If statement or where? please, clarify your answer.Sapphera
You should use the same email and username for Github, Gitlab and Bitbucket because when you reset email and username for repository, committer user name and email for every commit that you make will change based on the new email and username.Sapphera
@Ashvith Shetty Apologies; you're probably right about that. Initially, your answer didn't work for me, so When I did try my suggestion I think it did work. However, I had another underlying problem that I wasn't aware of until later on, and when I came back to use my suggestion, I think it didn't work. Ultimately what did work was pretty much in the same structure as your answer. I removed my previous suggestionHak
@Ashvitty Shetty I did write it here within the answers section actually (I also mentioned the side issue with BitBucket, you can copy it if you want) #76312780Hak
NB: the [includeIf <condition>] setting requires Git 2.36 or newer. I also had to specify specific path for config (path = ~/.gitlab in my case).Hiroshima
R
3

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?

Rabid answered 17/12, 2022 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.