Home Git setup for Different Login Credentials
Post
Cancel

Git setup for Different Login Credentials

Introduction

Sometimes you want to have two different credentials for working with two different repositories. For example lets say that you have a client that wants you to access their Azure account and you have your own personal github account.

Needless to say the two credentials for git will be different and you want to have a seamless experience is working with two different git credentials.

Enter .gitconfig to the rescue

.gitconfig allows you to control which folder in git uses which credentials. To use this first you have to create a ‘.gitconfig’ file in your home directory. Please note file does not have a name before the ‘.’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[color]
	ui = auto

[color "diff"]
	meta = yellow bold
	commit = green bold
	frag = magenta bold
	old = red bold
	new = green bold
	whitespace = red reverse

[core]
	editor = vim

    
[includeIf "gitdir:~/Work/01_git-repos/xyz/"]
    path = ~/Work/01_git-repos/xyz/.gitconfig.xyz
[includeIf "gitdir:~/Work/01_git-repos/abc/"]
    path = ~/Work/01_git-repos/abc/.gitconfig.abc
[includeIf "gitdir:~/Work/01_git-repos/public/"]
    path = ~/Work/01_git-repos/public/.gitconfig.personal

The main thing happening in the files is the includeIf statements. Basically they re-direct git depending on which folder git is operating in to use a different .gitconfig file. The way I have organised my folders is that for company ‘xyz’ the main folder is ~/Work/01_git-repos/xyz under which all the git projects related to that company is stored, whereas for the other company ‘abc’ git repository is located in ~/Work/01_git-repos/abc/.gitconfig.abc. For all other open source github projects the main folder is ~/Work/01_git-repos/public/.

Create SSH key-pairs

Before proceeding any further create your SSH keys. There is a good tutorial by github on how to do this. The ssh key pair files is usually located in this folder (~/.ssh/). For this tutorial I am assuming that there are 3 files by following names id_rsa_xyz (for company xyz), id_ed_abc (for company abc) and id_ed_my-github for your personal github ssh key. Please note it has been assumed that company xyz uses an older RSA based ssh key whereas company abc and your personal key is ed-25519. It does not make any difference as such except that you may want to name your ssh key files to remind you which files are generated using which algorithm as a best practice (hence the _rsa in the company xyz key name).

Create project specific .gitconfig files

So next in the ‘xyz’ company folder ~/Work/01_git-repos/xyz/ create a file ‘.gitconfig.xyz’ and copy the following lines into it. (change the name, email and id_rsa_xyz to the appropriate values depending on your actual file names).

1
2
3
4
5
6
[user]
    name = user-xyz
    email = user@xyz.com

[core]
sshCommand = "ssh -i ~/.ssh/id_rsa_xyz"

In the ‘abc’ company folder ~/Work/01_git-repos/abc/ create a file ‘.gitconfig.abc’ and copy the following lines into it.(change the name, email and id_rsa_xyz to the appropriate values depending on your actual file names).

1
2
3
4
5
6
[user]
    name = abc
    email = user@outlook.com

[core]
sshCommand = "ssh -i ~/.ssh/id_ed_abc"

Finally for the public repositories folder ~/Work/01_git-repos/public/ create a ‘.gitconfig.personal’ file and copy the following lines into it.(change the name, email and id_rsa_xyz to the appropriate values depending on your actual file names).

1
2
3
4
5
6
[user]
    name = my-github-name
    email = my-git-email@outlook.com

[core]
sshCommand = "ssh -i ~/.ssh/id_ed_my-github" 

Check

Clone a repository by going into the appropriate company folder. Once cloned cd into the cloned repository and use the following commands to list the git config used by git. (Note it only works inside an actual git repository). For example

1
2
3
4
cd ~/Work/01_git-repos/public/
git clone git@github.com:Digilent/digilent-xdc.git
cd digilent-xdc-master
git config --list

The last command should show the username, email address as well as the ssh key file used by git inside that folder.

Cherrio !!

This post is licensed under CC BY 4.0 by the author.

Docker Part 1 - Installation

Rotating Ring Stress Calculation