lilHar
lilHar

Reputation: 1856

Multiple usernames globally for different git repositories in the same project folder

I have two repository hubs, something like this:

gitlab.com/ and company_internal.example.com/opensource/projects

On gitlab, I use [email protected] with username Company Name and on the internal hub, I use [email protected] with Mrs. Mai Nhame

With each project, I'll have a setup something like this in each project's .git/config:

[remote "upstream"]
    url = ssh://git@gitlab/foo/bar.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
    name = Company Name
    email = [email protected]

[remote "origin"]
    url = ssh://company_internal.example.com/opensource/projects/bar.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
    name = Mrs. Mai Nhame
    email = [email protected]

However, I have to do this for every. single. project. And it's a lot of projects. Not to mention I have to remember it when creating a new project. Is there a way to globally set a different username/email depending on the git repo remote url?

The closest I've seen is the ability to change based on local directory, but in my scenario, the repos share directories and aren't in separate directories.

Upvotes: 0

Views: 90

Answers (2)

lilHar
lilHar

Reputation: 1856

After some experimenting, I found the closest solution I could do was the following:

I created the following script:

gclone:

#!/bin/bash
git clone $1
it remote add upstream $2 $3 $4

And I put it in my path. Now, instead of git clone ssh://company_internal.example.com/opensource/projects/bar.git and then trying to fiddle with the config later, I do gclone ssh://company_internal.example.com/opensource/projects/bar.git https:// MrsMaiNhameUsername @gitlab/foo/bar. . However, this only works with the https based repository urls that support usernaming in the url, and it doesn't help my existing repos (only going forward). But at least its a step in the right direction.

Upvotes: 0

bk2204
bk2204

Reputation: 76429

Git does not provide a way to set per-user configuration based on the URL. That's because the user.name and user.email settings are used when making commits, and Git doesn't know where you'll push those commits, if anywhere, when they're being made. You could push them to one place, multiple places, or nowhere, and in any event, those decisions would come after the fact. Note that these options control your personal name (that is, something other humans usually call you) and email address, but do not control usernames for connecting to remote servers or other interaction with remotes.

If you want to configure some settings based on the location on disk, you can do so like this:

[includeIf "gitdir:~/checkouts/work/"]
    path = ~/.gitconfig.work
[includeIf "gitdir:~/checkouts/personal/"]
    path = ~/.gitconfig.personal

You can then update those two files to contain the configuration settings, including user.name and user.email, for any repositories within those directories.

If you want to handle multiple usernames or credentials used to authenticate to remote servers, then the Git FAQ explains how to do that, both for HTTPS and for SSH.

Upvotes: 1

Related Questions