Abel Callejo
Abel Callejo

Reputation: 14929

How to specify multiple user.name and user.email for different git remotes?

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

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

Upvotes: 12

Views: 5895

Answers (5)

Skarlet
Skarlet

Reputation: 51

It's pretty simple if you need it for different projects.

You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run

git config user.name "Your Name Here"
git config user.email [email protected]

whereas the default user / email is configured in your ~/.gitconfig

git config --global user.name "Your Name Here"
git config --global user.email [email protected]

Upvotes: 0

Charles
Charles

Reputation: 4352

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:
  • 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 [email protected]: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

Upvotes: 1

diogo
diogo

Reputation: 771

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.

Upvotes: -1

VonC
VonC

Reputation: 1324367

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)

Upvotes: 2

ntshetty
ntshetty

Reputation: 1305

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.

Upvotes: -1

Related Questions