Setting up multiple account on git

You must have a default key for SSH connections between other hosts and can setup multiple SSH keys for each VCS account you have. You can use your personal key as a default key also.
In case you just to setup SSH connection and signing of commits for a single account skip to the bottom
Github
You can switch between two accounts on Github
Steps
Click on the profile icon
Click on
Add Accountto add your secondary account
Now you can switch between both profiles easily
Git
In order to use two profiles, you need to create two main directories. For example one personal and one work. Now keep in mind to clone/create repos accordingly. Personal repos must go into personal folder and similarly for work repos
Steps
- Generate SSH keys
ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f ~/.ssh/<personal_key>
ED25519 are recommended for security and performance, but RSA with a sufficient length is also a valid alternative:
ssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com" -f ~/.ssh/<personal_key>
-C is for adding a comment, it can be anything but it will be helpful to know the email associated with your account-f is to define a filename, and creating a key with a descriptive name will help you remember which key is used for which user/remote
Do this for you work mail
ssh-keygen -t rsa -b 4096 -C "your_personal_email@example.com" -f ~/.ssh/<work_key>
You can optionally add a parapharse
To update the passphrase for your SSH keys:
ssh-keygen -p -f ~/.ssh/<private_key>
- Adding the SSH Keys
First start the SSH agent
eval "$(ssh-agent -s)"
and then
ssh-add -K ~/.ssh/<personal_key>
ssh-add -K ~/.ssh/<work_key>
The -K flag in ssh-add is specific to macOS, and it adds the passphrase of the private key to the keychain. This means that once you've added the key to the keychain, you won't be asked for the passphrase again when using the key.
- Edit you SSH config
If you don't have a SSH config inside ~/.ssh, then create one using touch ~/.ssh/config
Edit it to the following
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<personal_key>
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/<work_key>
Here we are setting Host to * for every key because we will use Git configs to select the appropriate SSH key based on profiles, which will give us a bit more flexibility (more on that later).
- Add public keys to hosts To copy public key of the generated keys use this
pbcopy < ~/.ssh/<personal_key>.pub
The public key has been now copied to clipboard, you can use it in your desired VCS.
You can add the same SSH public key as a signing key also, you just have to specify that this this key is also used as a signing key in you VCS. Incase, you want to create another ssh key, follow the same steps and edit the necessary config options
- Updating your gitconfigs
This is how the the file structure should look like
/<some_folder>/
|__work/
|_.gitconfig-work
|__personal/
|_.gitconfig-personal
# ~/<path_to_personal_folder>/.gitconfig.pers
[user]
email = your_personal_email@example.com
name = Your Name
signingkey = ~/.ssh/<personal_key>.pub
[core]
sshCommand = "ssh -i ~/.ssh/<personal_key>"
# ~/<path_to_work_folder>/.gitconfig.work
[user]
email = your_work_email@google.com
name = Your Name
signingkey = ~/.ssh/<work_key>.pub
[core]
sshCommand = "ssh -i ~/.ssh/<work_key>"
Here we tell Git which key to use by making explicit the SSH command (the -i option is for specifying an identity file or key). This gives us more control, for instance, imagine you have multiple accounts on the same host (like github.com), if you were using the SSH config file to use different SSH keys, you would need to create and remember an arbitrary prefix like first.github.com and then remember to use that domain each time you want to clone a repository with this account. This can be too much trouble.
Updating your global gitconfig, which is present at ~/.gitconfig , i.e the home directory
[init]
defaultBranch = main
[gpg]
format = ssh
[commit]
gpgsign = true
[includeIf "gitdir:~/<path_to_work_folder>/"]
path = ~/<path_to_work_folder>/.gitconfig-work
[includeIf "gitdir:~/<path_to_personal_folder>/"]
path = ~/<path_to_personal_folder>/.gitconfig-personal
[core]
excludesfile = ~/.gitignore
If you just have one profile
Follow till Step 4, the update your global .gitconfig.
[init]
defaultBranch = main
[gpg]
format = ssh
[commit]
gpgsign = true
[user]
email = your_personal_email@google.com
name = Your Name
signingkey = ~/.ssh/<personal_key>.pub
[core]
sshCommand = "ssh -i ~/.ssh/<personal_key>"
excludesfile = ~/.gitignore
Notes
These steps are macOS specific in some cases, and will also work for Linux system.
For windows, the steps are same just commands are different.
This are my preferred config settings
