10

I have a repository in my local machine. This repository has 2 git remotes. Remotes A and B.

  • Remote A requires user.name X and user.email Y.
  • Remote B requires user.name Z and user.email W.

Can this be achieved in git? If so, how?

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
  • 2
    Make two repositories for the two remotes. Config `user.name` and `user.email` with `git config --local` in each repository. `user.name` and `user.email` have nothing to do with a remote. – ElpieKay Jul 14 '17 at 01:54
  • how about you have different username and email of your bitbucket and github accounts? you can't use them both at the same time as remote repositories? – Abel Callejo Jul 14 '17 at 06:42
  • 3
    `user.name` and `user.email` have nothing with remotes. They are only used to put commiter's name/email in commits, that's all. If you want to have different authorization for different remotes you can create different ssh keys and configure ssh to use each key for a different remote host. – phd Jul 14 '17 at 13:25
  • Does this answer your question? [How do I push to GitHub under a different username?](https://stackoverflow.com/questions/13103083/how-do-i-push-to-github-under-a-different-username) – miken32 May 09 '23 at 21:52
  • See also https://stackoverflow.com/questions/4220416/can-i-specify-multiple-users-for-myself-in-gitconfig – miken32 May 09 '23 at 21:52

4 Answers4

2

With Git 2.13, you now have conditional include for git config, but it is only based on filesystem path of the repository.

So you still need two different copies of the same repo. You can achieve that with git worktree (one clone, multiple working tree)

You can then modify your global config spec to include a local config with a specific set of user.name/user.email depending on your current folder.
See "Using different Git emails" from pltvs (Alex Pliutau)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Let me introduce everybody to a typical scenario:

Bob is a developer with a personal github user attached to his personal email address. Bob has a private dotfiles repository hosted on github.com. Bob gets a job at Acme corp and wants to share his dotfiles with coworkers. Bob uses his employer provisioned username and email to create a public dotfiles repository on an enterprise gitlab server only accessible from the corporate network at Acme Corp.

In this scenario the proper way to interact with these repositories is to configure your global git config on your work machine to use your work user.name and user.email:

  • git config --global user.name <work-username>
  • git config --global user.email <work-email>
  • On the work machine create a new key that can be used to access your public github:
    • ssh-keygen -t ed25519 -C "your-github-username:GITHUB" -f ~/.ssh/your-github-username*
  • Login to public github account in browser and add the newly created public key to your account (found at ~/.ssh/your-github-username.pub)
  • From work machine run:
    • git clone git@github.com:your-github-username/private-repo.git
  • Make a superfluous test commit, when you examine the repo in the browser you will see the commit was authored by your work user.name and user.email but pushed to your repository just fine.
  • Login to other/enterprise github/gitlab account (probably on a private server but not always) and create a new empty repository without a readme.
  • Run git remote add work git@domain.tld:work-username/repo.git
  • Push to your work repository with git push -u work

You can now push/pull from both your work repo and your personal repo. Don't worry about the usernames/emails not matching in the commits -- it honestly doesn't matter.

You can rename your remotes like so:

git remote rename origin home

git remote rename work employer

You should take care to regularly merge home into work and work into home to reconcile any differences since you shouldnt access work private network from home machine.

*this adds a comment to the key so you can remember what it’s for and also allows you to set an easy to remember name like your GitHub username

Charles D Pantoga
  • 4,307
  • 1
  • 15
  • 14
-1

I think you can achieve by doing something like this by using --local instead of --global

A git config user.email Y git config user.name "X"

B git config user.email W git config user.name "Z"

The values stored in the .git/config for particular repo than the global configuration file.

ntshetty
  • 1,293
  • 9
  • 20
-1

Say you have an account in gitlab with user name usergitlab with emails email@1, email@2, ... And that you also have an account in github with user name usergithub with emails email@2, email@3, ...

If your repo has a remote with two different urls, one for github:org/repo, another for gitlab:org/repo, and your local git email is email@2, then the name of the commit's author depends on the name of the user in gitlab and in github, that is, usergitlab and usergithub respectively.

diogo
  • 525
  • 1
  • 7
  • 12
  • This is not a good answer. It is very common for a person to have a personal repository such as a dotfiles repository that gets pushed to public git{lab,hub} and also have a remote setup for an enterprise git{lab,hub} server. In such cases you don't want to attach your work email to your personal github account and you can't attach your personal email account to your work account due to security policies. In that case you have a different username and email for each remote because your employer created your work username and email for you. – Charles D Pantoga Apr 29 '23 at 15:35