Bowen Peng
Bowen Peng

Reputation: 1825

How to specify different git account when visiting different ip or host?

I have to use different git account for the intranet and extranet for developing and software maintenance task.
For example, I have to set some proxies so I can ssh connect to the extranet but the extranet git account is different with the intranet one. So if set it global like git config --global user.name "user" it will cause some troubles when trying to ssh connect to git repository in the different web environment.
So I want to know how to set the different git account configuration for the different environment.
Here are my .gitconfig right now.

[http "http://git.intranet.com"]
    proxy = http://127.0.0.3:0303
[http "https://git.intranet.com"]
    proxy = http://127.0.0.3:0303
[user]
    name = bowen
    email = [email protected]

Could anyone help me and give some examples or hints?
Thanks in advances.

Upvotes: 3

Views: 2278

Answers (1)

Nithish
Nithish

Reputation: 6039

Creating SSH Config file

If you not having config file already then go ahead and create it.

cd ~/.ssh/
touch config
code config  //command code will be used to open this file in visual code

If config file is already available then add SSH configuration rules for different hosts so that the config will be helpful to pick the corresponding identity file to use for the provided domain.

# Intranet account--> default
Host github.com
   HostName github.com
   User <user>
   IdentityFile ~/.ssh/id_rsa

# Extranet account
Host github.com-extranet    
   HostName github.com
   User <user>
   IdentityFile ~/.ssh/id_rsa_extranet

Whatever is provided as Host is most important as this will be used for cloning or setting up the remote origin.

ssh-agent will use

  • id_rsa the key for any Git URL that uses @github.com
  • id_rsa_extranet the key for any Git URL that uses @github.com-extranet

In order to use intranet credentials for accessing the repo, execute the below commands

ssh-add -D              //removes all ssh entries from the ssh-agent
ssh-add ~/.ssh/id_rsa   // Adds the relevant ssh key

To push or access using extranet credentials

ssh-add -D
ssh-add ~/.ssh/id_rsa_extranet

For setting up/updating Extranet Repositories in local

  • To update the remote origin URL
git remote set-url origin [email protected]:<user>/repo_name.git
  • To init and add it as the Git remote to the local repository.
git init
git remote add origin [email protected]:<user>/repo_name.git

Hope this helps. For more info about ssh keys and all the process please go through this link

Upvotes: 2

Related Questions