Have you ever desired to manage multiple GitHub accounts on the same computer but lacked the knowledge on how to accomplish it? Fear not, for it is a simple task requiring only a small portion of code.
- In windows, open the “Services” app and search for OpenSSH Authentication Agent
- Right click over
OpenSSH Authentication Agent
, then Properties
- Set the startup type to
Automatic
Apply
changes and click start
. Then close it.
2. Create SSH Keys
- Open PowerShell
- Add command for the default account (personal recommended first)
1
| ssh-keygen -t rsa -b 4096 -C "juanpablodiaz"
|
- Enter the path location where you want to save the key (personal_key)
1
| C:\Users\juanc\.ssh\juanpablodiaz_key
|
- The key has been generated. To check, go to the folder where you save it.
- Add next GitHub account. (example: GitHub for work)
1
| ssh-keygen -t rsa -b 4096 -C "1diazdev"
|
- Enter the path location where you want to save the key
1
| C:\Users\juanc\.ssh\1diazdev_key
|
- The key has been generated. To check, go to the folder where you save it.
3. Create SSH Config File
- Open the
file explorer
in the folder where both keys are stored
- Create the
config
file
- Open the file in VScode and add the info below:
1
2
3
4
5
6
7
8
9
10
11
12
13
| # juanpablodiaz GitHub
Host juanpablodiaz
Hostname github.com
User git
IdentityFile ~/.ssh/juanpablodiaz_key
IdentitiesOnly yes
# 1diazdev GitHub
Host 1diazdev
Hostname github.com
User git
IdentityFile ~/.ssh/1diazdev_key
IdentitiesOnly yes
|
- Go to GitHub.com and login
- Go to
Menu
> Setting
> SSH and GPG keys
> new SSH keys
- Add the public key and save.
- Repet the process on the other GitHub account.
5. Add Keys to OpenSSH Cache
This is to prevent having to login to GitHub everytime I interact with GitHub.com.
- Open PowerShell and add the command (this will add the private key)
1
| ssh-add C:\Users\juanc\.ssh\juanpablodiaz_key
|
- Repet the process for the other account (adding the private key)
1
| ssh-add C:\Users\juanc\.ssh\1diazdev_key
|
- To test this add the code
- And
6. Preparing Folder Structure
- Create two folders inside a GitHub folder
- In the
Users\juanc
folder, Create 3 files:
.gitconfig
.gitconfig-juanpablodiaz
.gitconfig-1diazdev
- In my case, the
.gitconfig
file was already created.
- Open and replace it with the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [filter "lfs"]
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
required = true
[includeIf "gitdir:C:Users/juanc/Documents/Github/juanPabloDiaz"]
path = ./.gitconfig-juanpablodiaz
[includeIf "gitdir:C:\Users/juanc/Documents/Github/1diazdev"]
path = ./.gitconfig-1diazdev
[user]
name = Juan Diaz
email = juanchodis@hotmail.com
[core]
sshCommand = C:/Windows/System32/OpenSSH/ssh.exe
|
- Open the
.gitconfig-juanpablodiaz
and add the following code:
1
2
3
4
5
| [user]
name = juanpablodiaz
email = juanchodis@hotmail.com
[core]
sshCommand = ssh -i ~.ssh/juanpablodiaz_key
|
- Open the
.gitconfig-1diazdev
and add the following code:
1
2
3
4
5
| [user]
name = 1diazdev
email = juan.diaz.rodriguez93@gmail.com
[core]
sshCommand = ssh -i ~.ssh/1diazdev_key
|
7. Preparing Folder Structure
A. Link the existing repos to the new SSH
- Go to GitHub.com and find the repo
-
Copy the last part of the SSH link.
- SSH link:
git@github.com:JuanPabloDiaz/repoName.git
- Copy:
JuanPabloDiaz/repoName.git
- Add the SSH link to that especific folder repo
1
| git remote set-url origin juanpablodiaz:JuanPabloDiaz/repoName.git
|
Example:
1
2
3
| git remote set-url origin juanpablodiaz:JuanPabloDiaz/doc.git
or
git remote set-url origin 1diazdev:1diazdev/JuanDiaz.git
|
- Test it by doing a
git pull
Command to add a new repo from GitHub to local
1
| git clone juanpablodiaz:JuanPabloDiaz/repoName.git
|
or
1
| git clone 1diazdev:1diazdev/repoName.git
|
Test it by doing a git pull
Acknowledgments 📚
Resources list that I find helpful and would like to give credit to.