Automatically switch emails whilst using SSH
Asked Answered
M

1

0

I have an account on Github, Bitbucket, and Gitlab. Within my '.gitconfig' file, I have a setup like this, which involves Git automatically switching emails depending on the URL, looks something like this:

[includeIf "hasconfig:remote.*.url:https://github.com/**"]
  path = C:/Users/(User)/.gitconfig.github
[includeIf  "hasconfig:remote.*.url:https://gitlab.com:/**"]
  path = C:/Users/(User)/.gitconfig.gitlab
[includeIf "hasconfig:remote.*.url:https://bitbucket.org/**"]
  path = C:/Users/(User)/.gitconfig.bitbucket

This goes into a specific file and gets my username and email address being used on the designated platform (through which the email specifically, is different)

[user]
    name = (Name)
    email = (Email)

Now, how would I get this to work when I'm dealing with SSH keys, in which I would want Git to automatically switch emails depending on the platform I'm using? I've tried including things like:

[includeIf "hasconfig:remote.*.url:[email protected]:**"]
      path = C:/Users/(User)/.gitconfig.github

within the '.gitconfig' file, but as expected, it wasn't able to get the exact username and email address that's designated to the platform

Merylmes answered 23/5, 2023 at 7:34 Comment(4)
May I ask why do you want to have different emails for different hostings? Hostings are easily changed, esp, with DVCS like Git. What if you mirror a project from a hosting B to a hosting G? Will you change email in all commits? IMO includeIf should be used to distinguish personal emails from the work ones. May be different project emails. But hostings? Why?Dodds
@Dodds I use private emails for GitHub and GitLabMerylmes
Without any minor chance to copy a project between GitHub and GitLab?Dodds
@Dodds I use GitHub for public repositories, and GitLab for private ones, so the chances of me copying a project from GitLab to GitHub, or vice-versa, is low for now. And even then, I'd just probably take the long way around if I needed to copy a project from one platform to anotherMerylmes
M
0

Okay, I think I figured it out. Just a note for anyone who may encounter this error, check how you format the remote URL within the '.gitconfig' file. After a bit of research, I think it's supposed to be something like this:


(Using GitHub as an example...)

For Cloning Repositories with HTTPS:

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

For Cloning Repositories with SSH:

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

I believe this also prevents having to constantly use something like 'gitdir' to specify individual repositories within the '.gitconfig' file

Also, be aware when cloning with HTTPS with Bitbucket. It's structured differently compared to GitHub and GitLab; your username + '@' comes before 'bitbucket.org'. For example

[includeIf "hasconfig:remote.*.url:https://(YourBitbucketUsername)@bitbucket.org/**"]
Merylmes answered 23/5, 2023 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.